|
|
Contents
Overview 1
Using Constructors 2
Initializing Data 13
Lab 9.1: Creating Objects 31
Objects and Memory 39
Using Destructors 45
Lab 9.2: Destroying Objects 60
Review 65
Module 9: Creating and
Destroying Objects
This course is based on the prerelease Beta 1 version of Microsoft? Visual Studio .NET.
Content in the final release of the course may be different from the content included in
this prerelease version. All labs in the course are to be completed with the Beta 1
version of Visual Studio .NET.
Information in this document is subject to change without notice. The names of companies,
products, people, characters, and/or data mentioned herein are fictitious and are in no way intended
to represent any real individual, company, product, or event, unless otherwise noted. Complying
with all applicable copyright laws is the responsibility of the user. No part of this document may
be reproduced or transmitted in any form or by any means, electronic or mechanical, for any
purpose, without the expre ss written permission of Microsoft Corporation. If, however, your only
means of access is electronic, permission to print one copy is hereby granted.
Microsoft may have patents, patent applications, trademarks, copyrights, or other intellectual
property rights covering subject matter in this document. Except as expressly provided in any
written license agreement from Microsoft, the furnishing of this document does not give you any
license to these patents, trademarks, copyrights, or other intellectual property.
ó 2001 Microsoft Corporation. All rights reserved.
Microsoft, ActiveX, BizTalk, IntelliSense, JScript, Microsoft Press, MSDNPowerPoint, Visual
Basic, Visual C++, Visual C#, Visual Studio, Windows, and Windows Media are either registered
trademarks or trademarks of Microsoft Corporation in the U.S.A. and/or other countries.
Other product and company names mentioned herein may be the trademarks of their respective
owners.
Module 9: Creating and Destroying Objects 1
Overview
n Using Constructors
n Initializing Data
n Objects and Memory
n Using Destructors
In this module, you will learn what happens when an object is created, how to
use constructors to initialize objects, and how to use destructors to destroy
objects. You will also learn what happens when an object is destroyed and how
garbage collection reclaims memory.
After completing this module, you will be able to:
n Use constructors to initialize objects.
n Create overloaded constructors that can accept varying parameters.
n Describe the lifetime of an object and what happens when it is destroyed.
n Create destructors and use Finalize methods.
2 Module 9: Creating and Destroying Objects
u Using Constructors
n Creating Objects
n Using the Default Constructor
n Overriding the Default Constructor
n Overloading Constructors
Constructors are special methods that you use to initialize objects when you
create them. Even if you do not write a constructor yourself, a default
constructor is provided for you whenever you create an object from a reference
type. In this section, you will learn how to use constructors to control what
happens when an object is created.
Module 9: Creating and Destroying Objects 3
Creating Objects
n Step 1: Allocating Memory
l Use new operator to allocate memory from the heap
n Step 2: Initializing the Object with a Constructor
l Use the name of the class followed by parentheses
DDaattee wwhheenn == nneeww DDaattee(( ));;
s
The process of creating an object in C# involves two steps:
1. Use the new keyword to acquire and allocate memory for the object.
2. Write a constructor to turn the memory acquired by new into an object.
Even though there are two steps in this process, you must perform both steps in
one line of code. For example, if Date is the name of a class, use the following
syntax to allocate memory and initialize the object when.
Date when = new Date( );
Step 1: Allocating Memory
The first step in creating an object is to allocate memory for the object. All
objects are created by using the new operator. There are no exceptions to this
rule. You can do this explicitly in your code, or the compiler will do it for you.
In the following table, you can see examples of code and what they represent.
Code example Represents
string s = "Hello"; string s = new string("Hello" ;
int[ ] array = {1,2,3,4}; int[ ] array = new int[4]{1,2,3,4}; |
|