Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Daniel Solis - Illustrated C# 2010 - 2010.pdf
Скачиваний:
16
Добавлен:
11.06.2015
Размер:
11.23 Mб
Скачать

CHAPTER 6 MORE ABOUT CLASSES

Figure 6-13. The standard dispose pattern

Comparing Constructors and Destructors

Table 6-3 provides a summary of when constructors and destructors are called.

Table 6-3. Constructors and Destructors

 

 

When and How Often Called

Instance

Constructor

Called once on the creation of each new instance of the class.

 

Destructor

Called for each instance of the class, at some point after the program flow

 

 

can no longer access the instance.

Static

Constructor

Called only once—either before the first access of any static member of

 

 

the class or before any instances of the class are created, whichever is first.

 

Destructor

Does not exist—destructors work only on instances.

 

 

 

144

CHAPTER 6 MORE ABOUT CLASSES

The readonly Modifier

A field can be declared with the readonly modifier. The effect is similar to declaring a field as const, in that once the value is set, it cannot be changed.

While a const field can only be initialized in the field’s declaration statement, a readonly field can have its value set in any of the following places:

The field declaration statement—like a const.

Any of the class constructors. If it’s a static field, then it must be done in the static constructor.

While the value of a const field must be determinable at compile time, the value of a readonly field can be determined at run time. This additional freedom allows you to set different values under different circumstances or in different constructors!

Unlike a const, which always acts like a static, the following is true of a readonly field:

It can be either an instance field or a static field.

It has a storage location in memory.

145

CHAPTER 6 MORE ABOUT CLASSES

For example, the following code declares a class called Shape, with two readonly fields.

Field PI is initialized in its declaration.

Field NumberOfSides is set to either 3 or 4, depending on which constructor is called.

class Shape

 

 

 

 

 

{ Keyword

 

Initialized

 

 

 

 

 

 

readonly double PI = 3.1416;

 

readonly int

NumberOfSides;

 

 

 

 

 

 

 

 

Keyword

 

Not initialized

 

public Shape(double side1, double side2)

// Constructor

{

 

 

 

 

 

 

 

 

// Shape is a rectangle

 

 

NumberOfSides = 4;

 

 

 

 

 

 

 

 

 

 

... Set in constructor

 

}

 

 

 

 

 

 

 

public Shape(double side1, double side2, double side3)

// Constructor

{

 

 

 

 

 

 

 

 

// Shape is a triangle

 

NumberOfSides = 3;

... Set in constructor

}

}

146

CHAPTER 6 MORE ABOUT CLASSES

The this Keyword

The this keyword, used in a class, is a reference to the current instance. It can be used only in the blocks of the following class members:

Instance constructors.

Instance methods.

Instance accessors of properties and indexers. (Indexers are covered in the next section.)

Clearly, since static members are not part of an instance, you cannot use the this keyword inside the code of any static function member. Rather, it is used for the following:

To distinguish between class members and local variables or parameters

As an actual parameter when calling a method

For example, the following code declares class MyClass, with an int field and a method that takes a single int parameter. The method compares the values of the parameter and the field and returns the greater value. The only complicating factor is that the names of the field and the formal parameter are the same: Var1. The two names are distinguished inside the method by using the this access keyword to reference the field.

class MyClass

{

int Var1 = 10;

Both are called “Var1” ↓

public int ReturnMaxSum(int Var1)

 

{

Parameter

Field

 

 

 

 

 

return Var1 > this.Var1

 

 

? Var1

// Parameter

}

: this.Var1;

// Field

 

 

 

 

}

class Program

{

static void Main()

{

MyClass mc = new MyClass();

Console.WriteLine("Max: {0}", mc.ReturnMaxSum(30)); Console.WriteLine("Max: {0}", mc.ReturnMaxSum(5));

}

}

147

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]