Represents a value type that can be assigned nullptr.
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).)|
|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.|
|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 nullptr, which means the type has no value whatsoever. By default, all reference types, such as String, are nullable, but all value types, such as Int32, are not.
In C# and Visual Basic, you mark a value type as nullable by using the ? notation after the value type. For example, int? in C# or Integer? in Visual Basic declares an integer value type that can be assigned nullptr.
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.
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 nullptr. 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 nullptr. When nullptr is unboxed into a nullable type, the common language runtime creates a new structure and initializes its HasValue property to false.
.NET Framework 4.5.1 and Windows Runtime Components
Starting with the .NET Framework 4.5.1, you can include a type as a member of a structure exported in a WinMD library. Previously, this was not supported.
.NET FrameworkSupported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0
.NET Framework Client ProfileSupported in: 4, 3.5 SP1
XNA FrameworkSupported in: 3.0, 2.0, 1.0
Portable Class LibrarySupported in: Portable Class Library
Supported in: Windows Phone 8.1
Supported in: Windows Phone Silverlight 8.1
Supported in: Windows Phone Silverlight 8