可為 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>,以及指派 truefalsenullNullable<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 屬性傳回 false 的執行個體。The 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