Static Constructor In C#

  1. namespace Consolepractice   
  2. {  
  3.     class Program {  
  4.         public static void Main(string[] args) {  
  5.             Test.x = 1;  
  6.             //The static constructor for a class executes before any of the static members for the class are referenced.  
  7.             Console.WriteLine("static member value:" + Test.x);  
  8.             Test ob1 = new Test();  
  9.             Test ob2 = new Test();  
  10.             Console.WriteLine();  
  11.             Console.ReadKey();  
  12.         }  
  13.     }  
  14.     class Test {  
  15.         public static int x;  
  16.         public Test() // It will be executed every time when we create the object  
  17.             {  
  18.                 Console.WriteLine("Default Constructor");  
  19.             }  
  20.             //There should at most one Static Constructor in a class and also Static Constructor can't take any parameters or access modifiers.  
  21.             // It will be executed only once  
  22.             //The static constructor for a class executes before any instance of the class is created.(before default constructor)  
  23.         static Test() {  
  24.             Console.WriteLine("static Constructor");  
  25.         }  
  26.     }  
  27. // This is just a sample script. Paste your real code (javascript or HTML) here.  
  28. if ('this_is' == /an_example/) {  
  29.     of_beautifier();  
  30. else {  
  31.     var a = b ? (c % d) : e[f];