由网友(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不能值类型有默认的构造函数?
相关推荐
最新文章