为什么不能非静态字段进行内部结构初始化?字段、初始化、静态、内部结构

由网友(16.我在凌晨看日落)分享简介:考虑这个code座:struct Animal{public string name = ""; // Errorpublic static int weight = 20; // OK// initialize the non-static field herepublic void FuncToInitiali...

考虑这个code座:

struct Animal
{
    public string name = ""; // Error
    public static int weight = 20; // OK

    // initialize the non-static field here
    public void FuncToInitializeName()
    {
        name = ""; // Now correct
    }
}

为什么我们可以初始化一个静态字段中的非静态字段内的结构,但不? 为什么我们必须初始化非静态的方法机构?

Why can we initialize a static field inside a struct but not a non-static field? Why do we have to initialize non-static in methods bodies?

推荐答案

看一看Why不能值类型有默认的构造函数?

阅读全文

相关推荐

最新文章