可以为 null 的值类型(C# 编程指南)Nullable value types (C# Programming Guide)

可以为 null 的值类型是 System.Nullable<T> 结构的实例。Nullable value types are instances of the System.Nullable<T> structure. 可以为 null 的值类型可表示基础类型 T 的所有值,还可再表示一个 null 值。Nullable value types can represent all the values of an underlying type T, and an additional null value. 基础类型 T 可以是任何不可为 null 的值类型The underlying type T can be any non-nullable value type. T 不能是引用类型。T cannot be a reference type.

备注

C# 8.0 引入了可为空引用类型功能。C# 8.0 introduces the nullable reference types feature. 有关详细信息,请参阅可为空引用类型For more information, see Nullable reference types. 自 C# 2 起,提供可以为 null 的值类型。The nullable value types are available starting with C# 2.

例如,可以将 null 或任何整数值(从 Int32.MinValueInt32.MaxValue)赋给 Nullable<int>,并可将 truefalsenull 赋给Nullable<bool>For example, you can assign null or any integer value from Int32.MinValue to Int32.MaxValue to a Nullable<int> and true, false, or null to a Nullable<bool>.

需要表示基础类型的未定义的值时,请使用可以为 null 的值类型。You use a nullable value type when you need to represent the undefined value of an underlying type. 布尔变量只能有两个值:truefalseA Boolean variable can have only two values: true and false. 没有“未定义”的值。There is no "undefined" value. 在许多编程应用程序中,尤其是数据库交互中,变量值可能未定义或缺失。In many programming applications, most notably database interactions, a variable value can be undefined or missing. 例如,数据库中的字段可能包含值 true 或 false,但它也可能根本不包含任何值。For example, a field in a database may contain the values true or false, or it may contain no value at all. 这种情况下要使用 Nullable<bool> 类型。You use a Nullable<bool> type in that case.

可以为 null 的值类型具有以下特征:Nullable value types have the following characteristics:

  • 可以为 null 的值类型表示可向其赋值 null 的值类型变量。Nullable value types represent value-type variables that can be assigned the null value.

  • 语法 T?Nullable<T> 的简写。The syntax T? is shorthand for Nullable<T>. 这两种形式是可互换的。The two forms are interchangeable.

  • 向可以为 null 的值类型赋值的方法与向基础值类型赋值的方法相同:int? x = 10;double? d = 4.108;Assign a value to a nullable value type just as you would for an underlying value type: int? x = 10; or double? d = 4.108;. 还可赋予 null 值:int? x = null;You also can assign the null value: int? x = null;.

  • 使用 Nullable<T>.HasValueNullable<T>.Value 只读属性可测试是否存在 null 值并检索值,如以下示例所示:if (x.HasValue) y = x.Value;Use the Nullable<T>.HasValue and Nullable<T>.Value readonly properties to test for null and retrieve the value, as shown in the following example: if (x.HasValue) y = x.Value;

  • 还可将 ==!= 运算符用于可以为 null 的值类型,如以下示例所示:if (x != null) y = x.Value;You can also use the == and != operators with a nullable value type, as shown in the following example: if (x != null) y = x.Value;. 如果 ab 均为 null,则 a == b 的计算结果为 trueIf a and b are both null, a == b evaluates to true.

  • 从 C# 7.0 开始,可以使用模式匹配来检查和获取可以为 null 的类型的值:if (x is int valueOfX) y = valueOfX;Beginning with C# 7.0, you can use pattern matching to both examine and get a value of a nullable type: if (x is int valueOfX) y = valueOfX;.

  • T? 的默认值是一个实例,其 HasValue 属性返回 falseThe default value of T? is an instance whose HasValue property returns false.

  • 使用 GetValueOrDefault() 方法可返回赋予的值,如果可以为 null 的类型的值为 null,它还可返回基础值类型的默认值。Use the GetValueOrDefault() method to return either the assigned value, or the default value of the underlying value type if the value of the nullable type is null.

  • 使用 GetValueOrDefault(T) 方法可返回赋予的值,如果可以为 null 的类型的值为 null,它还可返回提供的默认值。Use the GetValueOrDefault(T) method to return either the assigned value, or the provided default value if the value of the nullable type is null.

  • 借助 null 合并运算符 ??,基于可以为 null 类型值向基础值类型的变量赋值:int? x = null; int y = x ?? -1;Use the null-coalescing operator, ??, to assign a value to a variable of an underlying value type based on a nullable-type value: int? x = null; int y = x ?? -1;. 在示例中,由于 xnull,因此 y 的结果值为 -1In the example, since x is null, the result value of y is -1.

  • 如果在两种值类型之间定义了用户定义的转换,则还可将同一转换用于相应的可以为 null 的类型。If a user-defined conversion is defined between two value types, the same conversion can also be used with the corresponding nullable types.

  • 不得嵌套可以为 null 的值类型。Nested nullable value types are not allowed. 不会编译下面的一行代码:Nullable<Nullable<int>> n;The following line doesn't compile: Nullable<Nullable<int>> n;

有关详细信息,请参阅使用可以为 null 的值类型以及如何:标识可以为 null 的值类型主题。For more information, see the Using nullable value types and How to: identify a nullable value type topics.

请参阅See also