Static field init : static field « Class « C# / CSharp Tutorial






using System;

public class MyClass
{
   private static int InitX()
   {
      Console.WriteLine( "MyClass.InitX()" );
      return 1;
   }
   private static int InitY()
   {
      Console.WriteLine( "MyClass.InitY()" );
      return 2;
   }
   private static int InitA()
   {
      Console.WriteLine( "MyClass.InitA()" );
      return 3;
   }
   private static int InitB()
   {
      Console.WriteLine( "MyClass.InitB()" );
      return 4;
   }

   private int y = InitY();
   private int x = InitX();

   private static int a = InitA();
   private static int b = InitB();
}

public class MainClass
{
   static void Main()
   {
      MyClass a = new MyClass();
   }
}
MyClass.InitA()
MyClass.InitB()
MyClass.InitY()
MyClass.InitX()








7.44.static field
7.44.1.Static Fields
7.44.2.Use a static field to count instances.
7.44.3.Use a static constructor.
7.44.4.Reference Static Field without Instance
7.44.5.Static field init
7.44.6.Reference a static member variable without using the class name
7.44.7.Instance Counting