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.


using System;
class Rectangle
{
    public static int Width = 5;
    static Rectangle()
    {
        Console.WriteLine("here");
    }
}
class Program
{
    static void Main(string[] args)
    {
        Console.WriteLine(Rectangle.Width);
    }
}

The output:


here
5
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.