Static Constructor

In this chapter you will learn:

  1. What is static constructor

What is static constructor

Each type can have only one static constructor. static constructor can have no parameters and it is execuated per type not per instance. static constructor is called by C# when initializing the type or calling its static members. It can only have extern and unsafe modifier.

A static constructor is used to initialize variables that apply to a class rather than an instance.

using System;// ja v a 2  s .  c o  m

class MyClass
{
    static MyClass()
    {
        Console.WriteLine("MyClass is initializing");
    }
    public static int I;
}

class MainClass{
    public static void Main()
    {
        MyClass.I = 1;
    }
}

The code above generates the following result.

The following code uses the static constructor to initialize the random generator.

using System;//  j a v a 2 s.com

class MyClass
{
   private static Random RandomKey;

   static MyClass()                     
   {
      RandomKey = new Random();                   
   }

   public int GetValue()
   {
      return RandomKey.Next();
   }
}

class Program
{
   static void Main()
   {
      MyClass a = new MyClass();
      MyClass b = new MyClass();
      Console.WriteLine("Next Random #: {0}", a.GetValue());
      Console.WriteLine("Next Random #: {0}", b.GetValue());
   }
}

The code above generates the following result.

The following code uses the static constructor to initialize static variables.

using System; /*from   ja v  a2  s  .  c  om*/
 
class Cons { 
  public static int a; 
  public int b; 
 
  // static constructor 
  static Cons() { 
    a = 99; 
    Console.WriteLine("Inside static constructor."); 
  } 
 
  // instance constructor 
  public Cons() { 
    b = 100; 
    Console.WriteLine("Inside instance constructor."); 
  } 
} 
  
class MainClass { 
  public static void Main() {   
    Cons ob = new Cons(); 
 
    Console.WriteLine("Cons.a: " + Cons.a); 
    Console.WriteLine("ob.b: " + ob.b); 
  } 
}

The code above generates the following result.

Next chapter...

What you will learn in the next chapter:

  1. How to create destructors
  2. Destructor calling sequence
  3. Update static field in the deconstructor
Home » C# Tutorial » Class
Classes and Structs
Encapsulation
Class and Struct Accessibility
Members
Static Members
Class
Class instance variables
Class instance Methods
Class Inheritance
Class constructor
Default constructors
Parameters of constructors
Constructor overloading
Static Constructor
Destructor
Override destructor
new operator
Class field
Static Fields
Read only field
Field default value
const value
Abstract class
Static Class
Partial type
Methods
Method local variable
Method Overloading
Method overloading with ref and out
Type conversion and method overloading
Error in Method overloading
virtual methods
Override methods
Method Parameter
Pass parameters
ref parameters
out parameters
Variable parameter length
Optional parameters
Named parameters
static method
function Return
Recursive methods
Virtual methods and polymorphism
Cast
Shadow inherited members
Seal a member
this keyword
base keyword
base and name hiding
Access Specifiers
private
public
protected
internal
Indexer
String type indexer
Multidimensional indexer
Indexer overloading
Indexer to append
Indexer logic
Properties
Properties getter and setter
Accessibilities of properties
Automatic properties
Read only and write only property
Virtual properties
Abstract Properties
Static Properties
interface
interface implementation
Explicit interface implementation
Interface inheritance
Indexer in an interface
Interface Properties
struct and interface
virtual interface implementation
as operator
is operator
null reference
Nested classes
object class
ToString method
GetHashCode
Object initialization
Extension method
Struct Instances vs. Class Instances