Wednesday 18 April 2012

C language..

Static Constructors

Just as an instance constructor guarantees that an object is in a well-defined initial state before it is used, a static constructor guarantees that a class is in a well-defined initial state before it is used.


Loading Classes at Run Time

C# is a dynamic language. When the common language runtime is running a .NET-based program, it often encounters code that uses a class that has not yet been loaded. In these situations, execution is momentarily suspended, the class is dynamically loaded, and then execution continues.

Initializing Classes at Load Time

C# ensures that a class is always initialized before it is used in code in any way. This guarantee is achieved by using static constructors.

We can declare a static constructor as you would an instance constructor, but prefix it with the keyword static, as follows: 

class Example
{
  static Example( ) { ... }
}


After the class loader loads a class that will soon be used, but before it continues normal execution, it executes the static constructor for that class. Because of this process, we are guaranteed that classes are always initialized before they are used. The exact timing of static constructor execution is implementation-dependent, but is subject to the following rules:
  • The static constructor for a class is executed before any instance of the class is created.
  • The static constructor for a class is executed before any static members of the class are referenced.
  • The static constructor for a class executes at most one time during a single program instantiation.

Static Field Initializations and Static Constructors

The most common use for a static constructor is to initialize the static fields of a class. This is because when we initialize a static field directly at its point of declaration, the compiler conceptually converts the initialization into an assignment inside the static constructor. In other words:

class Example
{
  private static Wibble w = new Wibble( );
}






is effectively converted by the compiler into

class Example
{
  static Example( )
  {
    w = new Wibble( );
  }


  private static Wibble w;
}
 

Static Constructor Restrictions

Understanding the following four restrictions on the syntax of static constructors will help you understand how the common language runtime uses static constructors:

We Cannot Call a Static Constructor

A static constructor must be called before any instances of the class are referenced in code. If the responsibility for enforcing this rule were given to programmers rather than the Microsoft .NET Framework runtime, eventually programmers would fail to meet the responsibility. They would forget to make the call, or, perhaps worse, they would call the static constructor more than once. The .NET runtime avoids these potential problems by disallowing calls to static constructors in code. Only the .NET runtime can call a static constructor.

class Point
{
  static Point( ) { ... }
  static void Main( )
  {
    Point.Point( ); // Compile-time error
  }
}
 

We Cannot Declare a Static Constructor with an Access Modifier

Because we cannot call a static constructor, declaring a static constructor with an access modifier does not make sense and causes a compile-time error:

class Point
{
  public static Point( ) { ... } // Compile-time error
}
 

We Cannot Declare a Static Constructor with Parameters

Because we cannot call a static constructor, declaring a static constructor with parameters does not make sense and causes a compile-time error. This also means that we cannot declare overloaded static constructors. Following is an example:

class Point
{
  static Point(int x) { ... } // Compile-time error
}
 

You Cannot Use the this Keyword in a Static Constructor

Because a static constructor initializes the class and not object instances, it does not have an implicit this reference, so any attempt to use the this keyword results in a compile-time error:

class Point
{
  private int x, y;
  static Point( ) : this(0,0) // Compile-time error
  {
    this.x = 0; // Compile-time error
    this.y = 0; // Compile-time error
  }
  ...
}

0 comments: