c# a piece of code for a static class

        class A
        {
    
    
            public static int X;
            static A()
            {
    
    
                X = B.Y + 1;
            }
        }

        class B
        {
    
    
            public static int Y = A.X + 1;
            static B() {
    
     }
            static void Main()
            {
    
    
                Console.WriteLine("X={0},Y={1}", A.X, B.Y);
            }
        }

Implementation process:

  1. Because there is a main function in class B, first initialize class B,
  2. Y=A.x+1; Ax is referenced, and the compiler initializes Class A
  3. The member variable x of class A, ——> the constructor A(), B is not initialized at this time, so By is 0, so Ax=1;
  4. Return to ClassB, continue to initialize the member variable Y, the value is 2.

Results of the
Insert picture description here

Guess you like

Origin blog.csdn.net/hhhhhhenrik/article/details/98376039