3

C#で非静的コンストラクターで静的クラス変数を初期化できるのはなぜですか? 静的変数は、静的コンストラクターでのみ初期化できるようにする必要があります。何か案は?

 public class customer
{
    public string Name;

    public customer()
    {
        Name = "C1";
        Console.WriteLine("creating customer " + Name);
    }

}

class Program
{
    public static customer cust;

    public Program()
    {
        cust = new customer(); //Why is this allowed i.e. initialize a static variable in non-static constructor?
    }

    static void Main(string[] args)
    {
        Program program = new Program();
        program = new Program();

        Console.Read();
    }
}
4

4 に答える 4