Nullable<T> 구조체
정의
null
에 할당할 수 있는 값 형식을 나타냅니다.Represents a value type that can be assigned null
.
generic <typename T>
where T : value classpublic value class Nullable
[System.Serializable]
public struct Nullable<T> where T : struct
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을 허용 하지 않은 두 개의 열과 두 개의 열이 null을 허용 하는 테이블에 포함 되어 있습니다.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을 허용 하지만 모든 값 형식의 경우와 같은 Int32, 되지 않습니다.By default, all reference types, such as String, are nullable, but all value types, such as Int32, are not.
C# 및 Visual Basic을 사용 하 여 null 허용으로 값 형식 표시는 ?
표기법 값 형식입니다.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을 허용 하기 때문에 값 형식만 nullable 형식으로 사용할 수 있도록 지원 합니다.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 클래스는 nullable 형식의 내부 형식 가져오기 지원 하며 같음 및 비교 작업 쌍 내부 값 형식이 nullable 형식에 제네릭 같음 및 비교 작업을 지원 하지 않습니다.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
두 기본 멤버를 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 및 unboxingBoxing and Unboxing
공용 언어 런타임 기본 값을 자동으로 상자 boxed nullable 형식이 인 경우는 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 속성 boxed 합니다.That is, if the HasValue property is true
, the contents of the Value property is boxed. 공용 언어 런타임 새로 만들고 nullable 형식의 기본 값이 boxed 때 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
nullable 형식의 속성은 false
, boxing 작업의 결과 null
합니다.If the HasValue
property of a nullable type is false
, the result of a boxing operation is null
. 따라서 boxed nullable 형식은 개체 인수를 필요로 하는 메서드에 전달 되 면 해당 메서드의 준비 해야 합니다 인수가 있는 경우를 처리 하도록 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 허용 형식으로 unboxed 일 공용 언어 런타임을 만듭니다 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. |