Lecture overview -- Keyboard shortcut: 'u'  Previous page: Copy constructors -- Keyboard shortcut: 'p'    Lecture notes - all slides and notes together  slide -- Keyboard shortcut: 't'  Textbook -- Keyboard shortcut: 'v'  Help page about these notes  Alphabetic index  Course home  Page 29 : 29
Object-oriented Programming in C#
Classes and Objects
Initialization of class variables

Constructors are deserved for initialization of instance variables. Class variable should be initialized before initialization of any instance variable.

It is too late - and not natural - to initialize class variables in ordinary constructors

  • Initialization of a class variable of type T (at class load time)

    • Via the default value of type T

    • Via the static field initializers

    • Via a static constructor

In most cases, we rely on initializers for initialization of class variables.

In some rare situations we need the power of static constructors. An example is shown below.

/user/normark/oop-csharp-1/sources/c-sharp/playing-card/playing-card-static-constructor/PlayingCard.csThe class PlayingCard with a static constructor. This program is explained

/user/normark/oop-csharp-1/sources/c-sharp/playing-card/playing-card-static-constructor/client.csA client of class PlayingCard.

/user/normark/oop-csharp-1/sources/c-sharp/playing-card/playing-card-static-constructor/outputOutput from the PlayingCard client program.

Read more about initialization of class variables in the text book version of this material.