C# 数组中成员的默认值是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15300073/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 16:21:54  来源:igfitidea点击:

What is the default value of a member in an array?

c#arraysdefault-valuemember

提问by dylanmensaert

I instantiate an array like this:

我像这样实例化一个数组:

int array[] = new int[4];

What are the default values for those four members? Is it null, 0 or not exists?

这四个成员的默认值是什么?它是空的,0 还是不存在?

采纳答案by Jon Skeet

It's 0. It can't be null, as null isn't a valid intvalue.

它是 0。它不能为 null,因为 null 不是有效值int

From section 7.6.10.4 of the C# 5 specification:

来自 C# 5 规范的第 7.6.10.4 节:

All elements of the new array instance are initialized to their default values (§5.2).

新数组实例的所有元素都初始化为其默认值(第 5.2 节)。

And from section 5.2:

从第 5.2 节:

The default value of a variable depends on the type of the variable and is determined as follows:

  • For a variable of a value-type, the default value is the same as the value computed by the value-type's default constructor (§4.1.2).
  • For a variable of a reference-type, the default value is null.

Initialization to default values is typically done by having the memory manager or garbage collector initialize memory to all-bits-zero before it is allocated for use. For this reason, it is convenient to use all-bits-zero to represent the null reference.

变量的默认值取决于变量的类型,确定如下:

  • 对于值类型的变量,默认值与值类型的默认构造函数(第 4.1.2 节)计算的值相同。
  • 对于引用类型的变量,默认值为空。

默认值的初始化通常是通过让内存管理器或垃圾收集器在分配使用之前将内存初始化为所有位为零来完成的。出于这个原因,使用所有位为零来表示空引用是很方便的。

(As an implementation detail, there's some trickiness around the first bullet point. Although C# itself doesn't allow you to declare a parameterless constructor for value types, you cancreate your own parameterless constructors for value types in IL. I don't believe those constructors are called in array initialization, but they willbe called in a new X()expression in C#. It's outside the realm of the C# spec though, really.)

(作为一个实现细节,围绕第一个要点有一些技巧。尽管 C# 本身不允许您为值类型声明无参数构造函数,但您可以在 IL 中为值类型创建自己的无参数构造函数。我不相信这些构造函数在数组初始化中被调用,但它们new X()在 C#中的表达式中被调用。不过,它确实超出了 C# 规范的范围。)

回答by Douglas

From Arrays (C# Programming Guide):

来自数组(C# 编程指南)

The default value of numeric array elements are set to zero, and reference elements are set to null.

数值数组元素的默认值设置为零,引用元素设置为空。

回答by Ron

Integers cannot be NULL. They will have the value '0'. Even if you try to assign NULLto a intfrom code you will not be able to do it.

整数不能是NULL. 它们的值为“0”。即使您尝试分配NULLintfrom 代码,您也将无法做到。

回答by Eric Lippert

The default value of an automatically-initialized variable of type T, such as an array element or an instance field, is the same as the value of default(T). For reference types and pointer types, it's null. For numeric types, it is the zero of that type. For bool, it's false. For struct types, it is the struct value that has all its fields initialized to their default values.

类型为自动初始化的变量(T例如数组元素或实例字段)的默认值与 的值相同default(T)。对于引用类型和指针类型,它为空。对于数字类型,它是该类型的零。对于 bool,它是错误的。对于 struct 类型,它的所有字段都初始化为其默认值的 struct 值。