使用英语阅读

通过


Nullable<T> 结构

定义

表示可分配有 null 的值类型。

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

类型参数

T

Nullable<T> 泛型类型的基础值类型。

继承
Nullable<T>
属性

示例

下面的代码示例定义 Microsoft Pubs 示例数据库中表的三行。 该表包含两个不可为 null 的列和两个可以为 null 的列。

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.

注解

有关此 API 的详细信息,请参阅可为 Null T<> 的补充 API 备注

构造函数

Nullable<T>(T)

Nullable<T> 结构的新实例初始化为指定值。

属性

HasValue

获取一个值,该值指示 Nullable<T> 对象是否具有基础类型的有效值。

Value

获取当前 Nullable<T> 对象的值,如果它已被分配了有效的基础值。

方法

Equals(Object)

指示当前 Nullable<T> 对象是否与指定的对象相等。

GetHashCode()

检索由 Value 属性返回的对象的哈希代码。

GetValueOrDefault()

检索当前 Nullable<T> 对象的值,或基础类型的默认值。

GetValueOrDefault(T)

检索当前 Nullable<T> 对象的值或指定的默认值。

ToString()

返回当前 Nullable<T> 对象的值的文本表示形式。

运算符

Explicit(Nullable<T> to T)

定义 Nullable<T> 实例到其基础值的显式转换。

Implicit(T to Nullable<T>)

创建一个新的 Nullable<T> 对象,并将其初始化为指定的值。

适用于

另请参阅