注意:這個結構是 .NET Framework 2.0 版的新功能。
表示其基礎型別為實值型別 (Value Type) 的物件,可以如同參考型別一樣指派 Null 參照 (即 Visual Basic 中的
Nothing)。
命名空間: System
組件: mscorlib (在 mscorlib.dll 中)
<SerializableAttribute> _
Public Structure Nullable(Of T As Structure)
Dim instance As Nullable(Of T)
[SerializableAttribute]
public struct Nullable<T> where T : struct
[SerializableAttribute]
generic<typename T> where T : ValueType
public value class Nullable
J# 可以支援使用泛型型別和方法,但不允許宣告新的泛型型別和方法。
如果型別可以指派值或 Null 參照 (即 Visual Basic 中的 Nothing) (表示型別沒有任何值),則這個型別稱為可為 null。因此,可為 null 的型別 (Nullable Type) 可以表示值,或表示值不存在。例如,參考型別 (如 String) 可為 null,而實值型別 (如 Int32) 則否。因為實值型別只有表示該型別適當值的足夠容量,並沒有表示 null 值所需的額外容量,所以不可為 null。
Nullable 結構只支援使用實值型別做為可為 null 的型別,因為參考型別在設計上是可為 null 的。
Nullable 類別為 Nullable 結構提供了互補支援。Nullable 類別支援取得可為 null 的型別之基礎型別,以及在可為 null 的型別 (其基礎實值型別並不支援泛型比較和相等作業) 之配對上的比較和相等作業。
案例
視情況而定,您可以使用可為 null 的型別表示存在或不存在的內容。例如,HTML 標記的選擇性屬性可能存在於某個標記,但不存在於其他標記;資料庫資料表的可為 null 資料行可能存在於資料表的某個資料列,但不存在於其他資料列。
您可以將屬性或資料行表示為類別中的欄位,然後將欄位定義為實值型別。此欄位可以包含屬性或資料行的所有有效值,但不可含有表示屬性或資料行不存在的其他值。在這個狀況下,您可以將欄位定義為可為 null 的型別,取代實值型別。
主要資料屬性
Boxing 和 Unboxing
當可為 null 的型別為 boxed,Common Language Runtime 就會自動 Box Nullable 物件的基礎值,而非 Nullable 物件本身。也就是說,如果 HasValue 屬性為 true,Value 屬性的內容即為 boxed。如果 HasValue 屬性為 false,則會 Box Null 參照 (即 Visual Basic 中的 Nothing)。當可為 null 的型別之基礎值為 unboxed,Common Language Runtime 就會建立初始化為基礎值的新 Nullable 結構。
下列程式碼範例在 Microsoft Pubs 範例資料庫中定義資料表的三個資料列。該資料表包含兩個不可為 null 的資料行和兩個可為 null 的資料行。
' This code example demonstrates the Nullable(Of T) class.
' The code example defines a database table in which two columns
' are nullable. In the application, an array of rows is created
' and initialized. The table rows could subsequently be
' written to a database.
Imports System
Class Sample
' Define the "titleAuthor" table of the Microsoft "pubs" database.
Public Structure titleAuthor
' Author ID; format ###-##-####
Public au_id As String
' Title ID; format AA####
Public title_id As String
' Author ORD is nullable.
Public au_ord As Nullable(Of Short)
' Royalty Percent is nullable.
Public royaltyper As Nullable(Of Integer)
End Structure 'titleAuthor
Public Shared Sub Main()
' Declare and initialize the titleAuthor array.
Dim ta(2) As titleAuthor
ta(0).au_id = "712-32-1176"
ta(0).title_id = "PS3333"
ta(0).au_ord = 1
ta(0).royaltyper = 100
ta(1).au_id = "213-46-8915"
ta(1).title_id = "BU1032"
ta(1).au_ord = Nothing
ta(1).royaltyper = Nothing
ta(2).au_id = "672-71-3249"
ta(2).title_id = "TC7777"
ta(2).au_ord = Nothing
ta(2).royaltyper = 40
' Display the values of the titleAuthor array elements, and
' display a legend.
Display("Title Authors Table", ta)
Console.WriteLine("Legend:")
Console.WriteLine("An Author ORD of -1 means no value is defined.")
Console.WriteLine("A Royalty % of 0 means no value is defined.")
End Sub 'Main
' Display the values of the titleAuthor array elements.
Public Shared Sub Display(ByVal dspTitle As String, _
ByVal dspAllTitleAuthors() As titleAuthor)
Console.WriteLine("*** {0} ***", dspTitle)
Dim dspTA As titleAuthor
For Each dspTA In dspAllTitleAuthors
Console.WriteLine("Author ID ... {0}", dspTA.au_id)
Console.WriteLine("Title ID .... {0}", dspTA.title_id)
Console.WriteLine("Author ORD .. {0}", dspTA.au_ord.GetValueOrDefault(-1))
Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper.GetValueOrDefault(0))
Console.WriteLine()
Next dspTA
End Sub 'Display
End Class 'Sample
'
'This code example produces the following results:
'
'*** Title Authors Table ***
'Author ID ... 712-32-1176
'Title ID .... PS3333
'Author ORD .. 1
'Royalty % ... 100
'
'Author ID ... 213-46-8915
'Title ID .... BU1032
'Author ORD .. -1
'Royalty % ... 0
'
'Author ID ... 672-71-3249
'Title ID .... TC7777
'Author ORD .. -1
'Royalty % ... 40
'
'Legend:
'An Author ORD of -1 means no value is defined.
'A Royalty % of 0 means no value is defined.
'
// This code example demonstrates the Nullable<T> class.
// The code example defines a database table in which two columns
// are nullable. In the application, an array of rows is created
// and initialized. The table rows could subsequently be
// written to a database.
using System;
class Sample
{
// Define the "titleAuthor" table of the Microsoft "pubs" database.
public struct titleAuthor
{
// Author ID; format ###-##-####
public string au_id;
// Title ID; format AA####
public string title_id;
// Author ORD is nullable.
public short? au_ord;
// Royalty Percent is nullable.
public int? royaltyper;
}
public static void Main()
{
// Declare and initialize the titleAuthor array.
titleAuthor[] ta = new titleAuthor[3];
ta[0].au_id = "712-32-1176";
ta[0].title_id = "PS3333";
ta[0].au_ord = 1;
ta[0].royaltyper = 100;
ta[1].au_id = "213-46-8915";
ta[1].title_id = "BU1032";
ta[1].au_ord = null;
ta[1].royaltyper = null;
ta[2].au_id = "672-71-3249";
ta[2].title_id = "TC7777";
ta[2].au_ord = null;
ta[2].royaltyper = 40;
// Display the values of the titleAuthor array elements, and
// display a legend.
Display("Title Authors Table", ta);
Console.WriteLine("Legend:");
Console.WriteLine("An Author ORD of -1 means no value is defined.");
Console.WriteLine("A Royalty % of 0 means no value is defined.");
}
// Display the values of the titleAuthor array elements.
public static void Display(string dspTitle,
titleAuthor[] dspAllTitleAuthors)
{
Console.WriteLine("*** {0} ***", dspTitle);
foreach (titleAuthor dspTA in dspAllTitleAuthors)
{
Console.WriteLine("Author ID ... {0}", dspTA.au_id);
Console.WriteLine("Title ID .... {0}", dspTA.title_id);
Console.WriteLine("Author ORD .. {0}", dspTA.au_ord ?? -1);
Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper ?? 0);
Console.WriteLine();
}
}
}
/*
This code example produces the following results:
*** Title Authors Table ***
Author ID ... 712-32-1176
Title ID .... PS3333
Author ORD .. 1
Royalty % ... 100
Author ID ... 213-46-8915
Title ID .... BU1032
Author ORD .. -1
Royalty % ... 0
Author ID ... 672-71-3249
Title ID .... TC7777
Author ORD .. -1
Royalty % ... 40
Legend:
An Author ORD of -1 means no value is defined.
A Royalty % of 0 means no value is defined.
*/
這個型別的所有公用靜態成員 (即 Visual Basic 中的 Shared 成員) 都是安全執行緒。並非所有的執行個體成員均為安全執行緒。
Windows 98、 Windows 2000 SP4、 Windows CE、 Windows Millennium Edition、 Windows Mobile for Pocket PC、 Windows Mobile for Smartphone、 Windows Server 2003、 Windows XP Media Center Edition、 Windows XP Professional x64 Edition、 Windows XP SP2、 Windows XP Starter Edition
.NET Framework 並不支援各種平台的所有版本。如需支援平台版本的相關資訊,請參閱系統需求一節的內容。
.NET Framework
支援版本:2.0
.NET Compact Framework
支援版本:2.0