15.02.2015 Views

C# 4 and .NET 4

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

76 ❘ ChaPTer 3 Objects And types<br />

{<br />

// initialization code<br />

}<br />

// rest of class definition<br />

}<br />

One reason for writing a static constructor is if your class has some static fields or properties that need to be<br />

initialized from an external source before the class is first used.<br />

The .<strong>NET</strong> runtime makes no guarantees about when a static constructor will be executed, so you should<br />

not place any code in it that relies on it being executed at a particular time (for example, when an assembly<br />

is loaded). Nor is it possible to predict in what order static constructors of different classes will execute.<br />

However, what is guaranteed is that the static constructor will run at most once, <strong>and</strong> that it will be<br />

invoked before your code makes any reference to the class. In <strong>C#</strong>, the static constructor is usually executed<br />

immediately before the first call to any member of the class.<br />

Notice that the static constructor does not have any access modifiers. It’s never called by any other <strong>C#</strong> code,<br />

but always by the .<strong>NET</strong> runtime when the class is loaded, so any access modifier such as public or private<br />

would be meaningless. For this same reason, the static constructor can never take any parameters, <strong>and</strong> there<br />

can be only one static constructor for a class. It should also be obvious that a static constructor can access<br />

only static members, not instance members, of the class.<br />

Note that it is possible to have a static constructor <strong>and</strong> a zero-parameter instance constructor defined in<br />

the same class. Although the parameter lists are identical, there is no conflict. That’s because the static<br />

constructor is executed when the class is loaded, but the instance constructor is executed whenever an<br />

instance is created — so there won’t be any confusion about which constructor gets executed when.<br />

Note that if you have more than one class that has a static constructor, the static constructor that will be<br />

executed first is undefined. This means that you should not put any code in a static constructor that depends<br />

on other static constructors having been or not having been executed. However, if any static fields have been<br />

given default values, these will be allocated before the static constructor is called.<br />

The next example illustrates the use of a static constructor <strong>and</strong> is based on the idea of a program that has<br />

user preferences (which are presumably stored in some configuration file). To keep things simple, we’ll<br />

assume just one user preference — a quantity called BackColor, which might represent the background<br />

color to be used in an application. And because we don’t want to get into the details of writing code to read<br />

data from an external source here, we’ll make the assumption that the preference is to have a background<br />

color of red on weekdays <strong>and</strong> green on weekends. All the program will do is display the preference in a<br />

console window — but this is enough to see a static constructor at work.<br />

namespace Wrox.ProCSharp.StaticConstructorSample<br />

{<br />

public class UserPreferences<br />

{<br />

public static readonly Color BackColor;<br />

static UserPreferences()<br />

{<br />

DateTime now = DateTime.Now;<br />

if (now.DayOfWeek == DayOfWeek.Saturday<br />

|| now.DayOfWeek == DayOfWeek.Sunday)<br />

BackColor = Color.Green;<br />

else<br />

BackColor = Color.Red;<br />

}<br />

}<br />

}<br />

private UserPreferences()<br />

{<br />

}<br />

www.it-ebooks.info

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!