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

可以为 null 的类型是 System.Nullable<T> 结构的实例。Nullable types are instances of the System.Nullable<T> struct. 可以为 null 的类型可表示一个基础类型的所有值 T,还可以再表示一个 null 值。Nullable 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.

例如,可以将 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 type when you need to represent the undefined value of an underlying type. 布尔变量只能有两个值:true 和 false。A 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 types have the following characteristics:

  • 可以为 null 的类型表示可以向其赋与 null 值的值类型变量。Nullable types represent value-type variables that can be assigned the null value. 不能根据引用类型创建可以为 null 的类型You cannot create a nullable type based on a reference type. (引用类型已支持 null 值)。(Reference types already support 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 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 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 an underlying type based on a value of the nullable type: int? x = null; int y = x ?? -1;. 在示例中,由于 x 为 null,所以 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 data types, the same conversion can also be used with the nullable versions of these data types.

  • 不得嵌套可以为 null 的类型。Nested nullable 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 types and How to: Identify a nullable type topics.

请参阅See also