Przeczytaj w języku angielskim

Udostępnij za pośrednictwem


Nullable<T> Struktura

Definicja

Reprezentuje typ wartości, który można przypisać null.

C#
public struct Nullable<T> where T : struct
C#
[System.Serializable]
public struct Nullable<T> where T : struct

Parametry typu

T

Podstawowy typ Nullable<T> wartości typu ogólnego.

Dziedziczenie
Nullable<T>
Atrybuty

Przykłady

Poniższy przykład kodu definiuje trzy wiersze tabeli w przykładowej bazie danych Microsoft Pubs. Tabela zawiera dwie kolumny, które nie są dopuszczające wartości null, i dwie kolumny, które można null.

C#
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.

Uwagi

Aby uzyskać więcej informacji na temat tego interfejsu API, zobacz Uwagi dotyczące dodatkowego interfejsu API dotyczące wartości Nullable<T>.

Konstruktory

Nullable<T>(T)

Inicjuje Nullable<T> nowe wystąpienie struktury do określonej wartości.

Właściwości

HasValue

Pobiera wartość wskazującą, czy bieżący Nullable<T> obiekt ma prawidłową wartość jego typu bazowego.

Value

Pobiera wartość bieżącego Nullable<T> obiektu, jeśli została przypisana prawidłowa wartość bazowa.

Metody

Equals(Object)

Wskazuje, czy bieżący Nullable<T> obiekt jest równy określonemu obiektowi.

GetHashCode()

Pobiera kod skrótu obiektu zwróconego przez Value właściwość.

GetValueOrDefault()

Pobiera wartość bieżącego Nullable<T> obiektu lub wartość domyślną typu bazowego.

GetValueOrDefault(T)

Pobiera wartość bieżącego Nullable<T> obiektu lub określoną wartość domyślną.

ToString()

Zwraca tekstowa reprezentacja wartości bieżącego Nullable<T> obiektu.

Operatory

Explicit(Nullable<T> to T)

Definiuje jawną konwersję Nullable<T> wystąpienia na jego wartość bazową.

Implicit(T to Nullable<T>)

Tworzy nowy Nullable<T> obiekt zainicjowany do określonej wartości.

Dotyczy

Produkt Wersje
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0

Zobacz też