namespace BaseTest { class test2 { //______________________________________________________________________ public static void Main( string[] args ) { //uncomment any - but only one - method at a time for each run //NoAccessAtAll(); // not invoked //AccessAnyField(); // invoked //InvokeStaticMethod(); // invoked //InstanceConstructor(); // invoked //FullAccessAnyField(); // invoked //FullInvokeStaticMethod(); // invoked //var xx = new Three(); // var xx = Three.xx; //var xx = new Four(); // not invoked //var yy = Four.InLine; // invoked Terminate(); } //______________________________________________________________________ static void NoAccessAtAll() { } //______________________________________________________________________ static void AccessAnyField() // constructor Invoked { string pp = StaticTest.AField; } //______________________________________________________________________ static void InvokeStaticMethod() { StaticTest.Hello(); } //______________________________________________________________________ static void InstanceConstructor() { var tt = new StaticTest(); } //______________________________________________________________________ static void FullAccessAnyField() // constructor Invoked { string pp = FullStaticTest.AField; } //______________________________________________________________________ static void FullInvokeStaticMethod() { FullStaticTest.Hello(); } //______________________________________________________________________ static void Terminate() { Console.Write("Any key top stop"); Console.ReadKey(); } //______________________________________________________________________ //______________________________________________________________________ //______________________________________________________________________ } //______________________________________________________________________ #if false // implied static class constructor .method private hidebysig specialname rtspecialname static void .cctor() cil managed { // Code size 21 (0x15) .maxstack 8 IL_0000: call string BaseTest.StaticTest::InitName() IL_0005: stsfld string BaseTest.StaticTest::Name IL_000a: ldstr "" IL_000f: stsfld string BaseTest.StaticTest::AField IL_0014: ret } // end of method StaticTest::.cctor #endif //______________________________________________________________________ class StaticTest { public static string Name = InitName(); public static string AField = ""; //______________________________________________________________________ static string InitName() { Console.WriteLine("Static Constructor InitName Invoked"); return "MyName"; } //______________________________________________________________________ public static string Hello() { return "Hello"; } } //______________________________________________________________________ static class FullStaticTest { public static string Name = InitName(); public static string AField = ""; //______________________________________________________________________ static string InitName() { Console.WriteLine("Static Constructor InitName Invoked"); return "MyName"; } //______________________________________________________________________ public static string Hello() { return "Hello"; } } //______________________________________________________________________ static class NoFields { static string xx; public static string Hello() { return "Hello"; } } //______________________________________________________________________ //______________________________________________________________________ class Three { public static string xx; static Three() { xx = "xx"; Console.WriteLine("Static Constructor Three Invoked"); } } //______________________________________________________________________ class Four { private static string _InLine; public static string InLine { get { if (_InLine == null) { Console.WriteLine("Static Constructor Four Inline Invoked"); _InLine = "xxx"; } return _InLine; } } } //______________________________________________________________________ }