默认值表(C# 引用)Default values table (C# reference)

下表显示 C# 类型的默认值:The following table shows the default values of C# types:

类型Type 默认值Default value
任何引用类型Any reference type null
任何内置整数数值类型Any built-in integral numeric type 0(零)0 (zero)
任何内置浮点型数值类型Any built-in floating-point numeric type 0(零)0 (zero)
boolbool false
charchar '\0' (U + 0000)'\0' (U+0000)
enumenum 表达式 (E)0 生成的值,其中 E 是枚举标识符。The value produced by the expression (E)0, where E is the enum identifier.
structstruct 通过如下设置生成的值:将所有值类型的字段设置为其默认值,将所有引用类型的字段设置为 nullThe value produced by setting all value-type fields to their default values and all reference-type fields to null.
任何可以为 null 的值类型Any nullable value type HasValue 属性为 falseValue 属性未定义的实例。An instance for which the HasValue property is false and the Value property is undefined. 该默认值也称为可以为 null 的值类型的 null 值 。That default value is also known as the null value of the nullable value type.

使用默认运算符生成默认类型值,如下面的示例所示:Use the default operator to produce the default value of a type, as the following example shows:

int a = default(int);

从 C# 7.1 开始,可使用default文本来初始化变量,使其具有其类型的默认值:Beginning with C# 7.1, you can use the default literal to initialize a variable with the default value of its type:

int a = default;

对于值类型,隐式无参数构造函数还可生成类型的默认值,如以下示例所示:For a value type, the implicit parameterless constructor also produces the default value of the type, as the following example shows:

var n = new System.Numerics.Complex();
Console.WriteLine(n);  // output: (0, 0)

C# 语言规范C# language specification

有关更多信息,请参阅 C# 语言规范的以下部分:For more information, see the following sections of the C# language specification:

请参阅See also