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. 例如, int?在C#或Integer?中 Visual Basic 声明可分配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. 如果属性为false ,null则为装箱。 HasValueIf 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.

适用于

另请参阅