为什么不必初始化数组中的结构? [英] Why doesn't a struct in an array have to be initialized?

查看:77
本文介绍了为什么不必初始化数组中的结构?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我研究了这个主题,但找不到任何重复的内容.我想知道为什么您可以在数组中使用 struct 而不创建其实例.

I researched this subject but I couldn't find any duplicate. I am wondering why you can use a struct in an array without creating an instance of it.

例如,我有一个 class 和一个 struct :

For example, I have a class and a struct:

public class ClassAPI
{
    public Mesh mesh { get; set; }
}

public struct StructAPI
{
    public Mesh mesh { get; set; }
}

在数组中使用 ClassAPI 时,必须先使用 new 关键字对其进行初始化,然后才能使用其属性和方法:

When ClassAPI is used in an array, it has to be initialized with the new keyword before being able to use its properties and methods:

ClassAPI[] cAPI = new ClassAPI[1];
cAPI[0] = new ClassAPI(); //MUST DO THIS!
cAPI[0].mesh = new Mesh();

但是 StructAPI 并非如此.看起来 StructAPI 不必在数组中初始化:

But this is not the case with StructAPI. It looks like StructAPI doesn't have to be initialized in the array:

StructAPI[] sAPI = new StructAPI[1];
sAPI[0].mesh = new Mesh();

如果使用 ClassAPI 尝试相同的操作,则会得到 NullReferenceException .

If you try the same thing with ClassAPI, you would get a NullReferenceException.

为什么在数组中使用结构时它们与结构有何不同?

Why is it different with structs when using them in an array?

我了解 class struct 之间的区别,其中 struct 是值类型,但这仍然没有意义.对我来说,如果没有涉及到数组,就好像我正在这样做:

I understand the difference between class and struct with struct being a value type but that still doesn't make sense. To me, without the array being involved in this, it would look like I am doing this:

StructAPI sp;
sp.mesh = new Mesh();

请注意,未初始化 sp 变量,它应导致出现编译时错误,提示:

Notice that the sp variable is not initialized and it should result in a compile-time error that says:

错误CS0165使用未分配的局部变量'sp'

Error CS0165 Use of unassigned local variable 'sp'

但是,当将 struct 放入数组时,情况就不同了.

but that's a different story when the struct is put in an array.

数组是否在其中初始化 struct ?我想知道发生了什么事.

Is the array initializing the struct in it? I would like to know what's going on.

推荐答案

按照规范链接,由PetSerAl在评论中提供:

As per the specification link provided by PetSerAl in the comments:

数组元素
数组的元素在创建数组实例时就存在,而在没有对该数组实例的引用时就不再存在.

Array elements
The elements of an array come into existence when an array instance is created, and cease to exist when there are no references to that array instance.

数组每个元素的初始值为默认值(

The initial value of each of the elements of an array is the default value (Default values) of the type of the array elements.

出于确定分配检查的目的,将数组元素视为初始分配.

For the purpose of definite assignment checking, an array element is considered initially assigned.

(重点是我的).

这意味着在声明 T 的数组时,将使用 default(T)初始化数组中的每个单元格".对于引用类型, default(T)返回 null ,但是对于值类型, default(T)返回类型的默认值– 0 表示数字, false 表示布尔,等等.

This means that when you declare an array of T, each "cell" in the array is being initialized using default(T). For reference types default(T) returns null, but for value types default(T) returns the type default value – 0 for numbers, false for bool, and so on.

按照使用结构(C#编程指南)页:

如果使用默认的无参数构造函数实例化struct对象,则会根据其默认值分配所有成员.

If you instantiate a struct object using the default, parameterless constructor, all members are assigned according to their default values.

由于结构是值类型,因此 default(T)其中 T 是一个结构,将结构初始化为其默认值,这意味着其所有成员都将被初始化为其默认值值–引用类型为 null ,值类型为任何默认值.

Since structs are value types, default(T) where T is a struct initializes the struct to its default value, meaning all its members will be initialized to their default values – null for reference types and whatever default value for value types.

因此这行代码 StructAPI [] sAPI = new StructAPI [1]; 基本上创建了一个新的 StructAPI 数组,其中包含一个 StructAPI 实例,其 mesh 属性为 default(Mesh).

So this line of code StructAPI[] sAPI = new StructAPI[1]; basically creates a new array of StructAPI containing a single StructAPI instance, where its mesh property is default(Mesh).

这篇关于为什么不必初始化数组中的结构?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆