小编nav*_*eed的帖子

为什么C#允许在非静态构造函数中初始化静态类变量?

为什么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();
    }
}
Run Code Online (Sandbox Code Playgroud)

c# variables static constructor initialization

3
推荐指数
1
解决办法
7223
查看次数

标签 统计

c# ×1

constructor ×1

initialization ×1

static ×1

variables ×1