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

CHAPTER 6 MORE ABOUT CLASSES

Static Constructors

Constructors can also be declared static. While an instance constructor initializes each new instance of a class, a static constructor initializes items at the class level. Generally, static constructors initialize the static fields of the class.

Class-level items need to be initialized:

Before any static member is referenced

Before any instance of the class is created

Static constructors are like instance constructors in the following ways:

The name of the static constructor must be the same as the name of the class.

The constructor cannot return a value.

Static constructors are unlike instance constructors in the following ways:

Static constructors use the static keyword in the declaration.

There can only be a single static constructor for a class, and it cannot have parameters.

Static constructors cannot have accessibility modifiers.

The following is an example of a static constructor. Notice that its form is the same as that of an instance constructor, but with the addition of the static keyword.

class Class1

 

{

 

static Class1 ()

 

{

 

...

// Do all the static initializations.

}

 

...

 

 

 

Other important things you should know about static constructors are the following:

A class can have both a static constructor and instance constructors.

Like static methods, a static constructor cannot access instance members of its class and cannot use the this accessor, which we’ll cover shortly.

You cannot explicitly call static constructors from your program. They’re called automatically by the system, at some time:

Before any instance of the class is created

Before any static member of the class is referenced

136

CHAPTER 6 MORE ABOUT CLASSES

Example of a Static Constructor

The following code uses a static constructor to initialize a private static field named RandomKey, of type Random. Random is a class provided by the BCL to produce random numbers. It is in the System namespace.

class RandomNumberClass

 

{

 

private static Random RandomKey;

// Private static field

static RandomNumberClass()

// Static constructor

{

 

RandomKey = new Random();

// Initialize RandomKey

}

 

public int GetRandomNumber()

 

{

 

return RandomKey.Next();

 

}

 

}

 

class Program

 

{

 

static void Main()

 

{

 

RandomNumberClass a = new RandomNumberClass();

RandomNumberClass b = new RandomNumberClass();

Console.WriteLine("Next Random #: {0}", a.GetRandomNumber()); Console.WriteLine("Next Random #: {0}", b.GetRandomNumber());

}

}

One execution of this code produced the following output:

Next Random #: 47857058

Next Random #: 1124842041

Accessibility of Constructors

You can assign access modifiers to instance constructors just as you can to other members. Notice that in the examples, the constructors have been declared public so that you can create instances from outside the class.

You can also create private constructors, which cannot be called from outside the class, but can be used from within the class, as you’ll see in the next chapter.

137

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