Nullable 类

定义

支持可分配有 null 的值类型。Supports a value type that can be assigned null. 无法继承此类。This class cannot be inherited.

public ref class Nullable abstract sealed
[System.Runtime.InteropServices.ComVisible(true)]
public static class Nullable
type Nullable = class
Public Class Nullable
继承
Nullable
属性

注解

如果可以为类型赋值或将其分配到 null,则称该类型为 null,这意味着该类型没有任何值。A type is said to be nullable if it can be assigned a value or can be assigned null, which means the type has no value whatsoever. 默认情况下,所有引用类型(如 String)都可以为 null,但所有值类型(如 Int32)都不是。By default, all reference types, such as String, are nullable, but all value types, such as Int32, are not.

在C#和 Visual Basic 中,通过在值类型后使用 ? 表示法,将值类型标记为可以为 null。In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. 例如,在 Visual Basic 中C# int?Integer? 声明可以 null分配的整数值类型。For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned null.

Nullable 类提供 Nullable<T> 结构的互补支持。The Nullable class provides complementary support for the Nullable<T> structure. Nullable 类支持获取可以为 null 的类型的基础类型,以及针对其基础值类型不支持泛型比较和相等运算的可以为 null 的类型对的比较和相等性运算。The Nullable class supports obtaining the underlying type of a nullable type, and comparison and equality operations on pairs of nullable types whose underlying value type does not support generic comparison and equality operations.

装箱和取消装箱Boxing and Unboxing

当对可以为 null 的类型进行装箱时,公共语言运行时将自动框 Nullable<T> 对象的基础值,而不是 Nullable<T> 对象本身。When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the Nullable<T> object, not the Nullable<T> object itself. 也就是说,如果 HasValue 属性为 true,则 Value 属性的内容为装箱。That is, if the HasValue property is true, the contents of the Value property is boxed. 如果 HasValue 属性为 false,则 null 为装箱。If the HasValue property is false, null is boxed. 如果可以为 null 的类型的基础值为取消装箱,则公共语言运行时将创建一个新的 Nullable<T> 结构,该结构已初始化为基础值。When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable<T> structure initialized to the underlying value.

方法

Compare<T>(Nullable<T>, Nullable<T>)

比较两个 Nullable<T> 对象的相对值。Compares the relative values of two Nullable<T> objects.

Equals<T>(Nullable<T>, Nullable<T>)

指示所指定的两个 Nullable<T> 对象是否相等。Indicates whether two specified Nullable<T> objects are equal.

GetUnderlyingType(Type)

返回指定可以为 null 的类型的基础类型参数。Returns the underlying type argument of the specified nullable type.

适用于

另请参阅