Vikash
Vikash

Reputation: 459

Static Constructor behaviour (C#)

How the static constructor is behaving here ?

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(String[] args)
    {
       Console.WriteLine("x={0} , y={1} ", a.x, b.y);
        Console.ReadLine();    
    }
}

Output ::

x=1 , y=2

How ?

Upvotes: 0

Views: 175

Answers (5)

Rune FS
Rune FS

Reputation: 21742

From the C# specifications

To execute the Main method, the system first runs the initializer for B.Y, prior to class B's static constructor. Y's initializer causes A's static constructor to be run because the value of A.X is referenced. The static constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, which is zero. A.X is thus initialized to 1. The process of running A's static field initializers and static constructor then completes, returning to the calculation of the initial value of Y, the result of which becomes 2.

the line

x = b.y + 1;

is executed as

x = 0 +1; 

because at this time it has not yet been possible to eveluate the value of b.y and the value of b.y is therefor it's default value (0)

the programs then continues to evaluate the statement

y = a.x + 1;

and since a.x was assign the value of 1 in the above statement the above is

y = 1 + 1;

the result in the end is that x == 1 and y == 2

Upvotes: 0

lc.
lc.

Reputation: 116488

  1. b.y is initialized, causing a to get initialized, so
  2. a() is called, setting a.x to b.y + 1 (b.y is still initialized to its default, 0)
  3. b.y is then set to a.x + 1 (a.x has been initialized to 1)

I would avoid this in real world code though...

Upvotes: 0

Rawling
Rawling

Reputation: 50114

This is, except for case, the exact example given here, which gives the following explanation:

To execute the Main method, the system first runs the initializer for B.Y, prior to class B's static constructor. Y's initializer causes A's static constructor to be run because the value of A.X is referenced. The static constructor of A in turn proceeds to compute the value of X, and in doing so fetches the default value of Y, which is zero. A.X is thus initialized to 1. The process of running A's static field initializers and static constructor then completes, returning to the calculation of the initial value of Y, the result of which becomes 2.

Upvotes: 7

Sjoerd
Sjoerd

Reputation: 75609

Remember that b.y starts out with the default value, 0. So while setting the value of y, a.x will become 1.

Upvotes: 0

Tigran
Tigran

Reputation: 62246

Everything starts from this line

public static int y = a.x + 1;

  • it goes to a.x
  • after to a type, so public static int x; initialized, and static

    static a()
    {
        x = b.y + 1;    
    }
    

ctor invoked.

  • In this ctor b.y is already staticaly initilized so we get b.y current value, which is 0,

so x = 0 + 1.

  • After we come back to public static int y = a.x + 1;

where a.x is 1, so it becomes int y = 1 + 1;

Upvotes: 4

Related Questions