Export (0) Print
Expand All

Nullable(T) Structure

Represents a value type that can be assigned a null reference (Nothing in Visual Basic).

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

[<Sealed>]
[<SerializableAttribute>]
type Nullable<'T when 'T : struct> =  struct end

Type Parameters

T

The underlying value type of the Nullable(T) generic type.

The Nullable(T) type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsNullable(T)Initializes a new instance of the Nullable(T) structure to the specified value.
Top

  NameDescription
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsHasValueGets a value indicating whether the current Nullable(T) object has a valid value of its underlying type.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsValueGets the value of the current Nullable(T) object if it has been assigned a valid underlying value.
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsEqualsIndicates whether the current Nullable(T) object is equal to a specified object. (Overrides ValueType.Equals(Object).)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetHashCodeRetrieves the hash code of the object returned by the Value property. (Overrides ValueType.GetHashCode().)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetValueOrDefault()Retrieves the value of the current Nullable(T) object, or the object's default value.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsGetValueOrDefault(T)Retrieves the value of the current Nullable(T) object, or the specified default value.
Public methodSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsToStringReturns the text representation of the value of the current Nullable(T) object. (Overrides ValueType.ToString().)
Top

  NameDescription
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsExplicit(Nullable(T) to T)Returns the value of a specified Nullable(T) value.
Public operatorStatic memberSupported by the XNA FrameworkSupported by Portable Class LibrarySupported in .NET for Windows Store appsImplicit(T to Nullable(T))Creates a new Nullable(T) object initialized to a specified value.
Top

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. 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 a null reference (Nothing in Visual Basic).

The Nullable(T) 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(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.

Fundamental Properties

The two fundamental members of the Nullable(T) structure are the HasValue and Value properties. If the HasValue property for a Nullable(T) 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 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. 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.

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 Nullable(T) 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 Nullable(T) type as a member of a structure exported in a WinMD library. Previously, this was not supported.

The following code example defines three rows of a table in the Microsoft Pubs sample database. The table contains two columns that are not nullable and two columns that are nullable.

No code example is currently available or this language may not be supported.

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone 8, Silverlight 8.1

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

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