I was stuck in between my code with the implementation of Static Code Initializers. Now, here I am calling the static field "x" of Classes A and B respectively in Main. Ideally, it should be generating the output as:
A : 0
A.x : 5
B : 0
B.x : 5
But, it is generating the output as:
A : 0
B : 0
A.x : 5
B.x : 5
Please explain.
Code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication2
{
public class A
{
public static A _A = new A();
public static int x = 5;
public A()
{
Console.WriteLine("A : " + x);
}
}
public class B
{
public static B _B = new B();
public static int x = 5;
public B()
{
Console.WriteLine("B : " + x);
}
}
class Program
{
static void Main(string[] args)
{
Console.WriteLine("A.x : " + A.x);
Console.WriteLine("B.x : " + B.x);
Console.ReadKey();
}
}
}