简体   繁体   English

在类vs结构中,new []到底在做什么?

[英]What exactly is new [] doing in a class vs struct?

MyClass[] CLASS = new MyClass[5];
int[] STRUCT = new int[5];

What exactly is new [] doing for a class vs a struct. new []对于类与结构到底是做什么的。 Apparently the struct has some overloaded static index that causes it to run the default constructor of the struct. 显然,该结构具有一些重载的静态索引,导致其运行该结构的默认构造函数。 However the new [] for a class appears to do nothing but make space to initialize an instance of a class. 但是,类的new []似乎不做任何事情,只是腾出空间来初始化类的实例。 How do I overload the static behavior of the class to also run the default constructor. 如何重载该类的静态行为以也运行默认构造函数。 I know how to use for loops and other methods of accomplishing this. 我知道如何使用循环和其他实现此目的的方法。 My question is very specific to what is going on underneath the new []. 我的问题非常具体,涉及新[]下的情况。 I understand that a struct needs a default value. 我知道结构需要默认值。 But doesn't a non-nullable class also need a default value which is why it gives an error when you try to use it? 但是非空类也不需要默认值吗,这就是为什么当您尝试使用它时会给出错误信息? Or is this telling me that all classes are actually nullable? 还是这告诉我所有类实际上都是可为空的?

No, the default constructor of struct isn't run. 不,默认的struct构造struct不会运行。 What happens instead is that in the struct case, the memory is initialized to zero. 而是在struct情况下将内存初始化为零。 What this means depends on the data type. 这意味着什么取决于数据类型。 Eg, a reference field (like a class or a string) becomes null , numeric fields become 0 , boolean fields become false , etc. 例如,参考字段(如类或字符串)变为null ,数字字段变为0 ,布尔字段变为false等。

This differs very much from how classes work. 这与类的工作方式有很大不同。 This is why with the initialization of the class array, you see null values. 这就是为什么在初始化class数组时会看到null值的原因。 Basically, it comes down to this. 基本上,可以归结为这一点。 An "empty" class variable (or array in your case) becomes null . “空” class变量(或您的情况下的数组)变为null However, when you have an "empty" struct , you already have something valid. 但是,当您具有“空” struct ,您已经有了一些有效的东西。 However, it is initialized as empty. 但是,它初始化为空。

The easiest way to see this is when you eg have an int field in a class. 最简单的方法是在类中有一个int字段时。 This works basically the same way. 基本上以相同的方式工作。 When you add an int field to a class, you are not required to initialize it. int字段添加到类时,不需要初始化它。 When you don't initialize it, it gets the value 0 by default. 不初始化时,默认情况下它的值为0 struct 's work the same way in this regard. 在这方面, struct的工作方式相同。

See http://msdn.microsoft.com/en-us/library/aa664471.aspx for a bit more information. 有关更多信息,请参见http://msdn.microsoft.com/zh-cn/library/aa664471.aspx

A few more things to note (answer to the question in the comments): 需要注意的其他事项(评论中问题的答案):

  • Classes are always nullable. 类始终是可为空的。 This means that the only way to initialize the array is to create a loop and initialize a new instance for every item in the array; 这意味着初始化数组的唯一方法是为数组中的每个项目创建一个循环并初始化一个新实例。
  • The default constructor doesn't apply to structs, because they aren't allowed to have a default constructor. 默认构造函数不适用于结构,因为不允许它们具有默认构造函数。 If you try to compile the following code snippet, you will get a compilation error telling you this: 如果您尝试编译以下代码段,则会出现编译错误告诉您:

     struct MyStruct { public MyStruct() { } } 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM