Partager via


Nullable Class

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Supports a value type that can be assigned nulla null reference (Nothing in Visual Basic) like a reference type. This class cannot be inherited.

Inheritance Hierarchy

System.Object
  System.Nullable

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)

Syntax

'Declaration
<ComVisibleAttribute(True)> _
Public NotInheritable Class Nullable
[ComVisibleAttribute(true)]
public static class Nullable

The Nullable type exposes the following members.

Methods

  Name Description
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Compare<T> Compares the relative values of two Nullable<T> objects.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 Equals<T> Indicates whether two specified Nullable<T> objects are equal.
Public methodStatic memberSupported by Silverlight for Windows PhoneSupported by Xbox 360 GetUnderlyingType Returns the underlying type argument of the specified nullable type.

Top

Remarks

A type is said to be nullable if it can be assigned a value or can be assigned nulla null reference (Nothing in Visual Basic), which means the type has no value whatsoever. Consequently, a nullable type can express a value, or that no value exists. For example, a reference type such as String is nullable, whereas a value type such as Int32 is not. A value type cannot be nullable because it has enough capacity to express only the values appropriate for that type; it does not have the additional capacity required to express a value of null.

The Nullable class provides complementary support for the Nullable<T> structure. 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.

Scenario

Use nullable types to represent things that exist or do not exist depending on the circumstance. For example, an optional attribute of an HTML tag might exist in one tag but not another, or a nullable column of a database table might exist in one row of the table but not another.

You can represent the attribute or column as a field in a class and you can define the field as a value type. The field can contain all the valid values for the attribute or column, but cannot accommodate an additional value that means the attribute or column does not exist. In this case, define the field to be a nullable type instead of a value type.

Boxing and Unboxing

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. That is, if the HasValue property is true, the contents of the Value property is boxed. If the HasValue property is false, nulla null reference (Nothing in Visual Basic) is boxed. 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.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

XNA Framework

Supported in: Xbox 360, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference