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

CHAPTER 12 STRUCTS

Constructors and Destructors

Structs can have instance and static constructors, but destructors are not allowed.

Instance Constructors

The language implicitly supplies a parameterless constructor for every struct. This constructor sets each of the struct’s members to the default value for that type. Value members are set to their default values. Reference members are set to null.

The predefined parameterless constructor exists for every struct—and you cannot delete or redefine it. You can, however, create additional constructors, as long as they have parameters. Notice that this is different from classes. For classes, the compiler will supply an implicit parameterless constructor only if no other constructors are declared.

To call a constructor, including the implicit parameterless constructor, use the new operator. Notice that the new operator is used even though the memory is not allocated from the heap.

For example, the following code declares a simple struct with a constructor that takes two int parameters. Main creates two instances of the struct—one using the implicit parameterless constructor and the second with the declared two-parameter constructor.

struct Simple

{

public int X; public int Y;

public

Simple(int a, int b)

// Constructor with parameters

{

 

 

 

 

X =

a;

 

Y =

b;

 

}

 

 

 

 

}

 

 

 

 

class Program

 

{

 

 

 

 

static

void Main()

 

{

Call implicit constructor

 

 

 

 

 

Simple s1 = new Simple();

Simple s2 = new Simple(5, 10);

Call constructor

Console.WriteLine("{0},{1}", s1.X, s1.Y); Console.WriteLine("{0},{1}", s2.X, s2.Y);

}

}

321

CHAPTER 12 STRUCTS

You can also create an instance of a struct without using the new operator. If you do this, however, there are some restrictions, which are the following:

You cannot use the value of a data member until you have explicitly set it.

You cannot call any function member of the struct until all the data members have been assigned.

For example, the following code shows two instances of struct Simple created without using the new operator. When there is an attempt to access s1 without explicitly setting the data member values, the compiler produces an error message. There are no problems reading from s2 after assigning values to its members.

struct Simple

{

public int X; public int Y;

}

 

 

 

 

class Program

 

 

 

{

 

 

 

 

static void Main()

 

 

{

No constructor calls

 

 

 

 

 

 

Simple s1, s2;

 

 

 

Console.WriteLine("{0},{1}", s1.X, s1.Y);

// Compiler error

 

 

 

 

s2.X = 5;

Not yet assigned

 

 

s2.Y = 10;

 

 

 

}

Console.WriteLine("{0},{1}", s2.X, s2.Y);

// OK

 

 

 

 

}

 

 

 

 

322

CHAPTER 12 STRUCTS

Static Constructors

As with classes, the static constructors of structs create and initialize the static data members and cannot reference instance members. Static constructors for structs follow the same rules as those for classes.

A static constructor is called before the first of either of the following two actions:

A call to an explicitly declared constructor

A reference to a static member of the struct

Summary of Constructors and Destructors

Table 12-1 summarizes the use of constructors and destructors with structs.

Table 12-1. Summary of Constructors and Destructors

Type

Description

 

 

Instance constructor (parameterless)

Cannot be declared in the program. An implicit

 

constructor is supplied by the system for all structs. It

 

cannot be deleted or redefined by the program.

Instance constructor (with parameters)

Can be declared in the program.

Static constructor

Can be declared in the program.

Destructor

Cannot be declared in the program. Destructors are not

 

allowed.

 

 

323

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