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

时间:2021-03-11 17:11:33

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?

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

4 个解决方案

#1


40  

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

它为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).
  • 对于value-type的变量,默认值与value-type的默认构造函数(第4.1.2节)计算的值相同。
  • For a variable of a reference-type, the default value is null.
  • 对于reference-type的变量,默认值为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.

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

(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 can create your own parameterless constructors for value types in IL. I don't believe those constructors are called in array initialization, but they will be called in a new X() expression in C#. It's outside the realm of the C# spec though, really.)

(作为一个实现细节,第一个要点周围有一些棘手的问题。虽然C#本身不允许你为值类型声明一个无参数构造函数,但你可以为IL中的值类型创建自己的无参数构造函数。我不相信这些构造函数在数组初始化中调用,但它们将在C#中的新X()表达式中调用。但它确实超出了C#规范的范围。)

#2


9  

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)的值相同。对于引用类型和指针类型,它为null。对于数字类型,它是该类型的零。对于布尔来说,这是错误的。对于struct类型,它是struct值,其所有字段都初始化为其默认值。

#3


4  

From Arrays (C# Programming Guide):

从数组(C#编程指南):

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

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

#4


0  

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

整数不能为NULL。它们的值为'0'。即使您尝试从代码中为int分配NULL,您也无法执行此操作。

#1


40  

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

它为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).
  • 对于value-type的变量,默认值与value-type的默认构造函数(第4.1.2节)计算的值相同。
  • For a variable of a reference-type, the default value is null.
  • 对于reference-type的变量,默认值为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.

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

(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 can create your own parameterless constructors for value types in IL. I don't believe those constructors are called in array initialization, but they will be called in a new X() expression in C#. It's outside the realm of the C# spec though, really.)

(作为一个实现细节,第一个要点周围有一些棘手的问题。虽然C#本身不允许你为值类型声明一个无参数构造函数,但你可以为IL中的值类型创建自己的无参数构造函数。我不相信这些构造函数在数组初始化中调用,但它们将在C#中的新X()表达式中调用。但它确实超出了C#规范的范围。)

#2


9  

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)的值相同。对于引用类型和指针类型,它为null。对于数字类型,它是该类型的零。对于布尔来说,这是错误的。对于struct类型,它是struct值,其所有字段都初始化为其默认值。

#3


4  

From Arrays (C# Programming Guide):

从数组(C#编程指南):

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

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

#4


0  

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

整数不能为NULL。它们的值为'0'。即使您尝试从代码中为int分配NULL,您也无法执行此操作。