Nullable<T> 構造体
定義
null
を割り当て可能な値型を表します。Represents a value type that can be assigned null
.
generic <typename T>
where T : value classpublic value class Nullable
public struct Nullable<T> where T : struct
[System.Serializable]
public struct Nullable<T> where T : struct
type Nullable<'T (requires 'T : struct)> = struct
[<System.Serializable>]
type Nullable<'T (requires 'T : struct)> = struct
Public Structure Nullable(Of T)
型パラメーター
- T
Nullable<T> ジェネリック型の基になる値型。The underlying value type of the Nullable<T> generic type.
- 継承
- 属性
例
次のコード例では、Microsoft Pubs サンプルデータベース内のテーブルの3つの行を定義します。The following code example defines three rows of a table in the Microsoft Pubs sample database. テーブルには、null 値を許容しない2つの列と null 値を許容する2つの列が含まれています。The table contains two columns that are not nullable and two columns that are nullable.
using namespace System;
// Define the "titleAuthor" table of the Microsoft "pubs" database.
value struct titleAuthor
{
public:
// Author ID; format ###-##-####
String^ au_id;
// Title ID; format AA####
String^ title_id;
// Author ORD is nullable.
Nullable<short> au_ord;
// Royalty Percent is nullable.
Nullable<int> royaltyper;
// Display the values of the titleAuthor array elements.
static void Display(String^ dspTitle,
array<titleAuthor>^ dspAllTitleAuthors)
{
Console::WriteLine("*** {0} ***", dspTitle);
for each (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.HasValue ?
dspTA.au_ord.Value : -1);
Console::WriteLine("Royalty % ... {0}", dspTA.royaltyper.HasValue ?
dspTA.royaltyper.Value : 0);
Console::WriteLine();
}
}
};
void main()
{
// Declare and initialize the titleAuthor array.
array<titleAuthor>^ ta = gcnew array<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 = nullptr;
// ta[1].royaltyper = nullptr;
ta[2].au_id = "672-71-3249";
ta[2].title_id = "TC7777";
// ta[2].au_ord = nullptr;
ta[2].royaltyper = 40;
// Display the values of the array elements, and
// display a legend.
titleAuthor::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.");
}
// The example displays the following output:
// *** 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.
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();
}
}
}
// The example displays the following output:
// *** 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.
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
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
' 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
End Sub
End Class
'This example displays the following output:
' *** 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.
注釈
型は、値を割り当てることができる場合、または割り当て可能な場合、null 値が許容されると言い null
ます。つまり、型には何の意味もありません。A type is said to be nullable if it can be assigned a value or can be assigned null
, which means the type has no value whatsoever. 既定では、などのすべての参照型 String は null 値を許容しますが、などのすべての値型は null 値を許容し Int32 ます。By default, all reference types, such as String, are nullable, but all value types, such as Int32, are not.
C# および Visual Basic では、値型の後に表記法を使用して、値型を nullable としてマークし ?
ます。In C# and Visual Basic, you mark a value type as nullable by using the ?
notation after the value type. たとえば、 int?
C# のまたは Integer?
では、Visual Basic 割り当て可能な整数値型を宣言 null
します。For example, int?
in C# or Integer?
in Visual Basic declares an integer value type that can be assigned null
.
Nullable<T>構造体は、参照型が設計によって null 許容であるため、null 許容型としての値型のみの使用をサポートします。The Nullable<T> structure supports using only a value type as a nullable type because reference types are nullable by design.
クラスは、 Nullable 構造体の相補的なサポートを提供し Nullable<T> ます。The Nullable class provides complementary support for the Nullable<T> structure. クラスは、 Nullable null 許容型の基になる型の取得、および基になる値の型が汎用的な比較および等値演算をサポートしていない null 許容型のペアに対する比較および等値演算をサポートします。The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.
基本プロパティFundamental Properties
構造体の2つの基本メンバー Nullable<T> は HasValue 、 Value プロパティとプロパティです。The two fundamental members of the Nullable<T> structure are the HasValue and Value properties. オブジェクトのプロパティがの場合は、 HasValue Nullable<T> プロパティを使用し true
てオブジェクトの値にアクセスでき Value ます。If the HasValue property for a Nullable<T> object is true
, the value of the object can be accessed with the Value property. HasValueプロパティがの場合 false
、オブジェクトの値は定義されてい Value ません。プロパティにアクセスしようとすると、がスローされ InvalidOperationException ます。If the HasValue property is false
, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.
ボックス化とボックス化解除Boxing and Unboxing
Null 許容型がボックス化されている場合、共通言語ランタイムは、オブジェクト自体ではなく、オブジェクトの基になる値を自動的にボックス化し Nullable<T> Nullable<T> ます。When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable<T> object, not the Nullable<T> object itself. つまり、 HasValue プロパティがの場合、 true
プロパティの内容 Value はボックス化されます。That is, if the HasValue property is true
, the contents of the Value property is boxed. Null 許容型の基になる値がボックス化解除されると、共通言語ランタイムは、 Nullable<T> 基になる値に初期化された新しい構造体を作成します。When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable<T> structure initialized to the underlying value.
HasValue
Null 許容型のプロパティがの場合 false
、ボックス化操作の結果はに null
なります。If the HasValue
property of a nullable type is false
, the result of a boxing operation is null
. その結果、ボックス化された null 許容型がオブジェクト引数を必要とするメソッドに渡される場合、そのメソッドは、引数がであるケースを処理できるように準備する必要があり null
ます。Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is null
. null
が null 許容型にボックス化解除されると、共通言語ランタイムは新しい Nullable<T> 構造体を作成し、その HasValue
プロパティをに初期化し false
ます。When null
is unboxed into a nullable type, the common language runtime creates a new Nullable<T> structure and initializes its HasValue
property to false
.
.NET Framework 4.5.1.NET Framework 4.5.1 および Windows ランタイムコンポーネントand Windows Runtime Components
以降では、 .NET Framework 4.5.1.NET Framework 4.5.1 Nullable<T> WinMD ライブラリにエクスポートされた構造体のメンバーとして型を含めることができます。Starting with the .NET Framework 4.5.1.NET Framework 4.5.1, you can include a Nullable<T> type as a member of a structure exported in a WinMD library. 以前は、これはサポートされていませんでした。Previously, this was not supported.
コンストラクター
Nullable<T>(T) |
Nullable<T> 構造体の新しいインスタンスを、指定の値に初期化します。Initializes a new instance of the Nullable<T> structure to the specified value. |
プロパティ
HasValue |
現在の Nullable<T> オブジェクトに、基になる型の有効値があるかどうかを示す値を取得します。Gets a value indicating whether the current Nullable<T> object has a valid value of its underlying type. |
Value |
有効な基になる値が割り当てられているかどうか示す、Nullable<T> の現在の値を取得します。Gets the value of the current Nullable<T> object if it has been assigned a valid underlying value. |
メソッド
Equals(Object) |
現在の Nullable<T> オブジェクトが指定されたオブジェクトと等しいかどうかを示します。Indicates whether the current Nullable<T> object is equal to a specified object. |
GetHashCode() |
Value プロパティから返されるオブジェクトのハッシュ コードを取得します。Retrieves the hash code of the object returned by the Value property. |
GetValueOrDefault() |
現在の Nullable<T> オブジェクトの値、または基になる型の既定値を取得します。Retrieves the value of the current Nullable<T> object, or the default value of the underlying type. |
GetValueOrDefault(T) |
現在の Nullable<T> オブジェクトの値、または指定した既定値を取得します。Retrieves the value of the current Nullable<T> object, or the specified default value. |
ToString() |
現在の Nullable<T> オブジェクトの値のテキスト形式を返します。Returns the text representation of the value of the current Nullable<T> object. |
演算子
Explicit(Nullable<T> to T) |
Nullable<T> インスタンスからその基になる値への明示的な変換を定義します。Defines an explicit conversion of a Nullable<T> instance to its underlying value. |
Implicit(T to Nullable<T>) |
指定の値に初期化された新しい Nullable<T> オブジェクトを作成します。Creates a new Nullable<T> object initialized to a specified value. |