Represents an object whose underlying type is a value type that can also be assigned a null reference (Nothing in Visual Basic) like a reference type.
Assembly: mscorlib (in mscorlib.dll)
Thetype exposes the following members.
|Equals||Indicates whether the current object is equal to a specified object. (Overrides ValueType.Equals(Object).)|
|Finalize||Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)|
|GetHashCode||Retrieves the hash code of the object returned by the Value property. (Overrides ValueType.GetHashCode().)|
|GetType||Gets the Type of the current instance. (Inherited from Object.)|
|GetValueOrDefault()||Retrieves the value of the current object, or the object's default value.|
|GetValueOrDefault(T)||Retrieves the value of the current object, or the specified default value.|
|MemberwiseClone||Creates a shallow copy of the current Object. (Inherited from Object.)|
|ToString||Returns the text representation of the value of the current object. (Overrides ValueType.ToString().)|
A type is said to be nullable if it can be assigned a value or can be assigned a 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 structure supports using only a value type as a nullable type because reference types are nullable by design.
The Nullable class provides complementary support for the 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.
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.
The two fundamental members of the structure are the HasValue and Value properties. If the HasValue property for a object is true, the value of the object can be accessed with the Value property. If the HasValue property is false, the value of the object is undefined and an attempt to access the Value property throws an InvalidOperationException.
Boxing and Unboxing
When a nullable type is boxed, the common language runtime automatically boxes the underlying value of the object, not the object itself. That is, if the HasValue property is true, the contents of the Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new structure initialized to the underlying value.
If the HasValue property of a nullable type is false, the result of a boxing operation is a null reference (Nothing in Visual Basic). Consequently, if a boxed nullable type is passed to a method that expects an object argument, that method must be prepared to handle the case where the argument is a null reference (Nothing in Visual Basic). When a null reference (Nothing in Visual Basic) is unboxed into a nullable type, the common language runtime creates a new structure and initializes its HasValue property to false.
Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2
The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.