Nullable<T> Struktúra
Definíció
Fontos
Egyes információk olyan, kiadás előtti termékekre vonatkoznak, amelyek a kiadásig még jelentősen módosulhatnak. A Microsoft nem vállal kifejezett vagy törvényi garanciát az itt megjelenő információért.
Hozzárendelhető nullértéktípust jelöl.
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ípusparaméterek
- T
Az általános típus mögöttes értéktípusa Nullable<T> .
- Öröklődés
- Attribútumok
Példák
Az alábbi példakód egy tábla három sorát határozza meg a Microsoft Pubs mintaadatbázisban. A táblázat két nem null értékű oszlopot és két null értékű oszlopot tartalmaz.
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.
Megjegyzések
Az Nullable osztály egy hozzárendelhető nullértéktípust jelöl.
A típus null értékűnek minősül, ha hozzárendelhető egy értékhez, vagy hozzárendelhető null, ami azt jelenti, hogy a típusnak nincs semmilyen értéke. Alapértelmezés szerint minden hivatkozástípus, például String, null értékű, de az összes értéktípus, például Int32nem.
A C#-ban és a Visual Basicben egy értéktípust null értékűként jelölhet meg az ? értéktípus utáni jelöléssel. Például a C#-ban int? vagy a Visual Basic-ben Integer? deklarál egy egész szám típusú értéket, amihez hozzárendelhető a null.
A Nullable<T> struktúra csak az értéktípusokat támogatja null értékű típusként, mivel a referenciatípusok a tervezés során null értékűek.
Az Nullable osztály kiegészítő támogatást nyújt a Nullable<T> struktúra számára. Az Nullable osztály támogatja a null értékű típus mögöttes típusának beszerzését, valamint az összehasonlítási és egyenlőségi műveleteket olyan null értékű típusok párjai esetében, amelyek mögöttes értéktípusa nem támogatja az általános összehasonlítási és egyenlőségi műveleteket.
Alapvető tulajdonságok
A Nullable<T> struktúra két alapvető tagja a HasValue és a Value tulajdonságok. Ha egy HasValueNullable<T> objektum tulajdonsága az true, az objektum értéke a tulajdonsággal Value érhető el. Ha a HasValue tulajdonság false, az objektum értéke nincs meghatározva, és a Value tulajdonság elérésére tett kísérlet InvalidOperationException kivételt dob.
Dobozolás és kicsomagolás
Amikor egy null értéket is tartalmazó típus dobozolva van, a közös nyelvi futtatókörnyezet automatikusan a Nullable<T> objektum mögöttes értékét dobozolja, nem magát az Nullable<T> objektumot. Vagyis ha a HasValue tulajdonság az true, akkor a Value tulajdonság tartalma be van jelölve. Amikor egy nullable típus mögöttes értéke kibontásra kerül, a közös nyelvi futtatókörnyezet létrehoz egy új Nullable<T> struktúrát, amely az alapul szolgáló értékkel van inicializálva.
Ha egy nullable típus HasValue tulajdonsága false, akkor a boxing művelet eredménye null lesz. Következésképpen, ha egy dobozolt, null értékű típust adnak át egy olyan metódusnak, amely objektumargumentumot vár, akkor a metódusnak fel kell készülnie arra, hogy kezelje az esetet, amikor az argumentum null. Amikor a null ki van csomagolva egy null értékű típusba, a közös nyelvi futtatókörnyezet új Nullable<T> struktúrát hoz létre, és a HasValue tulajdonságát inicializálja a false értékkel.
Windows-futtatókörnyezet összetevői
Egy WinMD-kódtárba exportált struktúra tagjaként megadhat egy Nullable<T> típust.
Konstruktorok
| Name | Description |
|---|---|
| Nullable<T>(T) |
Inicializálja a Nullable<T> struktúra új példányát a megadott értékre. |
Tulajdonságok
| Name | Description |
|---|---|
| HasValue |
Beolvas egy értéket, amely jelzi, hogy az aktuális Nullable<T> objektum rendelkezik-e a mögöttes típusú érvényes értékkel. |
| Value |
Lekéri az aktuális Nullable<T> objektum értékét, ha érvényes mögöttes érték van hozzárendelve. |
Metódusok
| Name | Description |
|---|---|
| Equals(Object) |
Azt jelzi, hogy az aktuális Nullable<T> objektum egyenlő-e egy adott objektummal. |
| GetHashCode() |
Lekéri a tulajdonság által visszaadott objektum kivonatkódját Value . |
| GetValueOrDefault() |
Lekéri az aktuális Nullable<T> objektum értékét vagy az alapul szolgáló típus alapértelmezett értékét. |
| GetValueOrDefault(T) |
Lekéri az aktuális Nullable<T> objektum vagy a megadott alapértelmezett érték értékét. |
| ToString() |
Az aktuális Nullable<T> objektum értékének szöveges ábrázolását adja vissza. |
Operátorok
| Name | Description |
|---|---|
| Explicit(Nullable<T> to T) |
Egy példány explicit átalakítását Nullable<T> határozza meg annak mögöttes értékére. |
| Implicit(T to Nullable<T>) |
Létrehoz egy új Nullable<T> objektumot egy megadott értékre inicializálva. |