小知識:.Net中?和??的含義

      int? demo = null;          // int?的含義是什麼
      int result = demo ?? -5;   // ??的含義是什麼
      result = (int)(result + demo);
      Console.WriteLine(result);


T? 表示 System.Nullable<T>,int ?表示System.Nullable<int>;

“demo ?? -5” 表示在demo爲null時返回-5

下面引用自MSDN:

對於System.Nullable泛型結構,表示基礎類型爲值類型的對象,值類型與引用類型一樣也可以分配 空引用(在 Visual Basic 中爲 Nothing)。

對於一個類型,如果既可以給它分配一個值,也可以給它分配 空引用(在 Visual Basic 中爲 Nothing)(表示沒有任何值),我們就說這個類型是可空的。因此,可空類型可表示一個值,或表示不存在任何值。例如,類似 String 的引用類型就是可空類型,而類似 Int32 的值類型不是可空類型。由於值類型的容量只夠表示適合於該類型的值,因此它不可爲空;值類型沒有表示空值所需的額外容量。

Nullable 結構支持只將一個值類型用作可空類型,因爲引用類型本身就是可空的。

Nullable 類爲 Nullable 結構提供補充支持。Nullable 類支持獲取可空類型的基礎類型,以及對基礎值類型不支持一般的比較和相等性操作的可空類型進行成對的比較和相等性操作。

方案

根據不同的應用場合,可使用可空類型來表示存在或不存在的內容。例如,HTML 標記的某個可選屬性可能存在於某一個標記中,但不存在於另一個標記中;或者數據庫表的某個可空列可能存在於表的某一行中,但不存在於另一行中。

可將屬性或列表示爲類中的字段,並且可將該字段定義爲值類型。該字段可包含屬性或列的所有有效值,但不能提供一個附加值來表示屬性或列不存在。在這種情況下,應將該字段定義爲可空類型,而不是值類型。

基本屬性

Nullable 結構的兩個基礎成員爲 HasValueValue 屬性。如果 Nullable 對象的 HasValue 屬性爲 true,則可以使用 Value 屬性訪問該對象的值。如果 HasValue 屬性爲 false,則表示尚未定義該對象的值,並且嘗試訪問 Value 屬性時會引發 InvalidOperationException

裝箱和取消裝箱

在對可空類型進行裝箱時,公共語言運行庫自動將 Nullable 對象的基礎值(而不是 Nullable 對象本身)裝箱。也就是說,如果 HasValue 屬性爲 true,則將 Value 屬性的內容裝箱。如果 HasValue 屬性爲 false,則將空引用(在 Visual Basic 中爲 Nothing)裝箱。在對可空類型的基礎值進行取消裝箱時,公共語言運行庫創建一個新的初始化爲基礎值的 Nullable 結構。

// This code example demonstrates the Nullable<T> class.
// The code example defines a database table in which two columns 
// are nullable. In the application, an array of rows is created 
// and initialized. The table rows could subsequently be 
// written to a database.

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();       
       }
    }
}

/*
This code example produces the following results:

*** 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.

*/
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章