Nullable<T> Struktur
Definition
Wichtig
Einige Informationen beziehen sich auf Vorabversionen, die vor dem Release ggf. grundlegend überarbeitet werden. Microsoft übernimmt hinsichtlich der hier bereitgestellten Informationen keine Gewährleistungen, seien sie ausdrücklich oder konkludent.
Stellt einen Werttyp dar, der null zugewiesen werden kann.
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)
Typparameter
- T
Der zugrunde liegende Werttyp des generischen Nullable<T>-Typs.
- Vererbung
- Attribute
Beispiele
Im folgenden Codebeispiel werden drei Zeilen einer Tabelle in der Beispieldatenbank von Microsoft Pubs definiert. Die Tabelle enthält zwei Spalten, die nicht nullfähig sind, und zwei Spalten, die nullfähig sind.
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.
open System
// Define the "titleAuthor" table of the Microsoft "pubs" database.
type titleAuthor =
struct
// Author ID format ###-##-####
val mutable au_id: string
// Title ID format AA####
val mutable title_id: string
// Author ORD is nullable.
val mutable au_ord: Nullable<int16>
// Royalty Percent is nullable.
val mutable royaltyper: Nullable<int>
end
// Display the values of the titleAuthor array elements.
let display dspTitle (dspAllTitleAuthors: #seq<titleAuthor>) =
printfn $"*** {dspTitle} ***"
for dspTA in dspAllTitleAuthors do
printfn $"Author ID ... {dspTA.au_id}"
printfn $"Title ID .... {dspTA.title_id}"
printfn $"Author ORD .. {dspTA.au_ord.GetValueOrDefault -1s}"
printfn $"Royalty %% ... {dspTA.royaltyper.GetValueOrDefault -1}\n"
// Declare and initialize the titleAuthor array.
let ta = Array.zeroCreate<titleAuthor> 3
ta[0].au_id <- "712-32-1176"
ta[0].title_id <- "PS3333"
ta[0].au_ord <- Nullable 1s
ta[0].royaltyper <- Nullable 100
ta[1].au_id <- "213-46-8915"
ta[1].title_id <- "BU1032"
ta[1].au_ord <- Nullable()
ta[1].royaltyper <- Nullable()
ta[2].au_id <- "672-71-3249"
ta[2].title_id <- "TC7777"
ta[2].au_ord <- Nullable()
ta[2].royaltyper <- Nullable 40
// Display the values of the titleAuthor array elements, and
// display a legend.
display "Title Authors Table" ta
printfn "Legend:"
printfn "An Author ORD of -1 means no value is defined."
printfn "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.
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.
Hinweise
Ein Typ soll nullfähig sein, wenn er einem Wert zugewiesen werden kann oder zugewiesen nullwerden kann, was bedeutet, dass der Typ keinen Wert hat. Standardmäßig sind alle Bezugstypen, z String. B. nullfähig, aber alle Werttypen, z Int32. B. , nicht.
In C# und Visual Basic markieren Sie einen Werttyp als Nullwert, indem Sie die ? Notation nach dem Werttyp verwenden. In C# oder Integer? in Visual Basic wird beispielsweise int? ein ganzzahliger Werttyp deklariert, der zugewiesen nullwerden kann.
Die Nullable<T> Struktur unterstützt die Verwendung eines Werttyps als nullablen Typ, da Bezugstypen vom Entwurf nullfähig sind.
Die Nullable Klasse bietet ergänzende Unterstützung für die Nullable<T> Struktur. Die Nullable Klasse unterstützt das Abrufen des zugrunde liegenden Typs eines nullablen Typs sowie Vergleichs- und Gleichheitsvorgänge für Paare mit nullablen Typen, deren zugrunde liegender Werttyp keine generischen Vergleichs- und Gleichheitsvorgänge unterstützt.
Grundlegende Eigenschaften
Die beiden grundlegenden Elemente der Nullable<T> Struktur sind die HasValue und Value die Eigenschaften. Wenn die HasValue Eigenschaft für ein Nullable<T> Objekt lautet true, kann auf den Wert des Objekts mit der Value Eigenschaft zugegriffen werden. Wenn die HasValue Eigenschaft lautet false, wird der Wert des Objekts nicht definiert, und ein Versuch, auf die Value Eigenschaft zuzugreifen, löst eine InvalidOperationException.
Boxing und Unboxing
Wenn ein nullabler Typ boxt, ordnet die allgemeine Sprachlaufzeit automatisch den zugrunde liegenden Wert des Nullable<T> Objekts, nicht das Nullable<T> Objekt selbst, ein. Das heißt, wenn die HasValue Eigenschaft lautet true, wird der Inhalt der Value Eigenschaft boxt. Wenn der zugrunde liegende Wert eines nullfähigen Typs unboxed ist, erstellt die allgemeine Sprachlaufzeit eine neue Nullable<T> Struktur, die für den zugrunde liegenden Wert initialisiert wurde.
Wenn die HasValue Eigenschaft eines nullfähigen Typs lautet, ist false``nulldas Ergebnis eines Boxvorgangs . Wenn daher ein boxter nullabler Typ an eine Methode übergeben wird, die ein Objektargument erwartet, muss diese Methode darauf vorbereitet sein, den Fall zu behandeln, in dem sich das Argument befindet null. Wenn null der Posteingang in einem nullfähigen Typ aufgehoben wird, erstellt die allgemeine Sprachlaufzeit eine neue Nullable<T> Struktur und initialisiert seine HasValue Eigenschaft auf false.
.NET Framework 4.5.1 und Windows-Runtime Komponenten
Ab dem .NET Framework 4.5.1 können Sie einen Nullable<T> Typ als Element einer Struktur einschließen, die in einer WinMD-Bibliothek exportiert wird. Zuvor wurde dies nicht unterstützt.
Konstruktoren
| Nullable<T>(T) |
Initialisiert eine neue Instanz der Nullable<T>-Struktur mit dem angegebenen Wert. |
Eigenschaften
| HasValue |
Ruft einen Wert ab, der angibt, ob das aktuelle Nullable<T>-Objekt einen gültigen Wert des zugrunde liegenden Typs hat. |
| Value |
Ruft den Wert des aktuellen Nullable<T>-Objekts ab, wenn ihm ein gültiger zugrunde liegender Wert zugewiesen wurde. |
Methoden
| Equals(Object) |
Gibt an, ob das aktuelle Nullable<T>-Objekt einem angegebenen Objekt entspricht. |
| GetHashCode() |
Ruft den Hashcode des Objekts ab, das von der Value-Eigenschaft zurückgegeben wird. |
| GetValueOrDefault() |
Ruft den Wert des aktuellen Nullable<T>-Objekts oder den Standardwert des zugrunde liegenden Typs ab. |
| GetValueOrDefault(T) |
Ruft den Wert des aktuellen Nullable<T>-Objekts oder den angegebenen Standardwert ab. |
| ToString() |
Gibt die Textdarstellung des Werts des aktuellen Nullable<T>-Objekts zurück. |
Operatoren
| Explicit(Nullable<T> to T) |
Definiert eine explizite Konvertierung einer Nullable<T>-Instanz in den zugrunde liegenden Wert. |
| Implicit(T to Nullable<T>) |
Erstellt ein neues Nullable<T>-Objekt, das mit einem angegebenen Wert initialisiert wurde. |