<分区>
我在我的代码和静态代码初始化器的实现之间陷入困境。现在,我在这里分别调用Main中A类和B类的静态字段“x”。理想情况下,它应该生成如下输出:
A : 0
A.x : 5
B : 0
B.x : 5
但是,它生成的输出为:
A : 0
B : 0
A.x : 5
B.x : 5
请解释。
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();
}
}
}