Mega Code Archive

 
Categories / C# Book / 01 Language Basics
 

0113 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