Nullable<T> Yapı

Tanım

nullatanabilen bir değer türünü temsil eder.

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ür Parametreleri

T

Genel türün Nullable<T> temel değer türü.

Devralma
Nullable<T>
Öznitelikler

Örnekler

Aşağıdaki kod örneği, Microsoft Pubs örnek veritabanında bir tablonun üç satırını tanımlar. Tabloda null değer atanamayan iki sütun ve null değer atanabilen iki sütun bulunur.

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.

Açıklamalar

sınıfı, Nullable atanabilen nullbir değer türünü temsil eder.

Bir türe bir değer atanabiliyorsa veya null atanabiliyorsa, bu türün null atanabilir olduğu söylenir. Bu, türün hiçbir değeri olmadığı anlamına gelir. Varsayılan olarak, tüm başvuru türleri, örneğin String, null atanabilir, ancak tüm değer türleri, örneğin Int32, null atanamaz.

C# ve Visual Basic'te, değer türünü nullable olarak işaretlemek için değer türünün ardından ? gösterimini kullanırsınız. Örneğin, int? C# veya Integer? Visual Basic'te atanabilecek nullbir tamsayı değer türü bildirir.

Yapı Nullable<T>, başvuru türleri tasarım gereği zaten null atanabilir olduğundan, null atanabilir tür olarak yalnızca bir değer türü kullanmayı destekler.

Nullable sınıfı, Nullable<T> yapısı için tamamlayıcı destek sağlar. The Nullable sınıfı, null atanabilir bir türün temel türünü almayı destekler ve temel değer türü genel karşılaştırma ve eşitlik işlemlerini desteklemeyen null atanabilir tür çiftleri üzerinde karşılaştırma ve eşitlik işlemlerini gerçekleştirir.

Temel özellikler

Yapının iki temel üyesi Nullable<T> ve HasValue özellikleridirValue. Bir nesnenin HasValue özelliği iseNullable<T>, nesnenin değerine özelliğiyle trueValue erişilebilir. HasValue özelliği isefalse, nesnenin değeri tanımlanmamıştır ve özelliğine erişme Value girişimi bir InvalidOperationExceptionoluşturur.

Kutulama ve kutu açma

Boş değer atanabilir bir tür kutulandığında, ortak dil çalışma zamanı, Nullable<T> nesnesinin kendisini değil, nesnenin Nullable<T> temel değerini otomatik olarak kutular. Yani, özellik HasValue ise, true özelliğinin Value içeriği kutulanır. Null atanabilir bir türün temel alınan değeri kutusundan çıkarıldığında, ortak dil çalışma zamanı temel alınan değerle başlatılan yeni bir Nullable<T> yapısı oluşturur.

Eğer null atanabilir bir türün HasValue özelliği false ise, kutulama işleminin sonucu null olur. Sonuç olarak, bir nesne bağımsız değişkeni bekleyen bir yönteme kutulanmış null atanabilir bir tür geçirilirse, bu yöntem bağımsız değişkenin nullolduğu durumu işlemeye hazır olmalıdır. Boş değer atanabilir bir türe dönüştürüldüğünde null, ortak dil çalışma zamanı yeni bir Nullable<T> yapısı oluşturur ve HasValue özelliğini false olarak başlatır.

Windows çalışma zamanı bileşenleri

WinMD kitaplığında dışarı aktarılan bir yapının üyesi olarak bir Nullable<T> türü ekleyebilirsiniz.

Oluşturucular

Name Description
Nullable<T>(T)

Belirtilen değere yapısının Nullable<T> yeni bir örneğini başlatır.

Özellikler

Name Description
HasValue

Geçerli Nullable<T> nesnenin temel alınan türünde geçerli bir değere sahip olup olmadığını gösteren bir değer alır.

Value

Geçerli bir temel değer atanmışsa geçerli Nullable<T> nesnenin değerini alır.

Yöntemler

Name Description
Equals(Object)

Geçerli Nullable<T> nesnenin belirtilen bir nesneye eşit olup olmadığını gösterir.

GetHashCode()

özelliği tarafından Value döndürülen nesnenin karma kodunu alır.

GetValueOrDefault()

Geçerli Nullable<T> nesnenin değerini veya temel alınan türün varsayılan değerini alır.

GetValueOrDefault(T)

Geçerli Nullable<T> nesnenin veya belirtilen varsayılan değerin değerini alır.

ToString()

Geçerli Nullable<T> nesnenin değerinin metin gösterimini döndürür.

İşleçler

Name Description
Explicit(Nullable<T> to T)

Bir örneğin temel alınan değerine açık bir Nullable<T> dönüşümünü tanımlar.

Implicit(T to Nullable<T>)

Belirtilen değere başlatılan yeni Nullable<T> bir nesne oluşturur.

Şunlara uygulanır

Ayrıca bkz.