System.Nullable<T> Structure

Represents a value type that can be assigned null.

See Also: Nullable<T> Members


public struct Nullable<T>
where T : struct

Type Parameters

Documentation for this section has not yet been entered.


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. By default, all reference types, such as string, are nullable, but all value types, such as int, 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 null.

The Nullable`1 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 Nullable`1 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.

Fundamental Properties

The two fundamental members of the Nullable`1 structure are the Nullable`1.HasValue and Nullable`1.Value properties. If the Nullable`1.HasValue property for a Nullable`1 object is true, the value of the object can be accessed with the Nullable`1.Value property. If the Nullable`1.HasValue property is false, the value of the object is undefined and an attempt to access the Nullable`1.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 Nullable`1 object, not the Nullable`1 object itself. That is, if the Nullable`1.HasValue property is true, the contents of the Nullable`1.Value property is boxed. When the underlying value of a nullable type is unboxed, the common language runtime creates a new Nullable`1 structure initialized to the underlying value.

If the HasValue property of a nullable type is false, the result of a boxing operation is null. 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 null. When null is unboxed into a nullable type, the common language runtime creates a new Nullable`1 structure and initializes its HasValue property to false.

net_v451 and Windows Runtime Components

Starting with the net_v451, you can include a Nullable`1 type as a member of a structure exported in a WinMD library. Previously, this was not supported.


Namespace: System
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions:,
Since: .NET 2.0