C# two classes with static members referring to each other

Posted by Jerry on Stack Overflow See other posts from Stack Overflow or by Jerry
Published on 2010-05-06T21:16:04Z Indexed on 2010/05/06 21:18 UTC
Read the original article Hit count: 288

Filed under:

Hi,

I wonder why this code doesn't end up in endless recursion. I guess it's connected to the automatic initialization of static members to default values, but can someone tell me "step by step" how does 'a' get the value of 2 and 'b' of 1?

public class A
{
    public static int a = B.b + 1;
}
public class B
{
    public static int b = A.a + 1;
}

static void Main(string[] args)
{
    Console.WriteLine("A.a={0}, B.b={1}", A.a, B.b); //A.a=2, B.b=1
    Console.Read();
}

© Stack Overflow or respective owner

Related posts about static-members