Skip to main content
.NET Framework Class Library
NullableT Structure

Represents a value type that can be assigned .

Namespace:   System
Assembly:  mscorlib (in mscorlib.dll)
Syntax
<[%$TOPIC/b3h38hb0_en-us_VS_110_1_0_0_0_0%]> _
Public Structure Nullable(Of T As Structure)
[[%$TOPIC/b3h38hb0_en-us_VS_110_1_0_1_0_0%]]
public struct Nullable<T>
where T : struct
[[%$TOPIC/b3h38hb0_en-us_VS_110_1_0_2_0_0%]]
generic<typename T>
where T : value class 
public value class Nullable
[<[%$TOPIC/b3h38hb0_en-us_VS_110_1_0_3_0_0%]>]
[<[%$TOPIC/b3h38hb0_en-us_VS_110_1_0_3_0_1%]>]
type Nullable<'T when 'T : struct> =  struct end

Type Parameters

T

The underlying value type of the NullableT generic type.

The NullableT type exposes the following members.

Constructors
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps NullableTInitializes a new instance of the NullableT structure to the specified value.
Top
Properties
  NameDescription
Public property Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps HasValueGets a value indicating whether the current NullableT object has a valid value of its underlying type.
Public property Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ValueGets the value of the current NullableT object if it has been assigned a valid underlying value.
Top
Methods
  NameDescription
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps EqualsIndicates whether the current NullableT object is equal to a specified object. (Overrides ValueTypeEquals(Object).)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetHashCodeRetrieves the hash code of the object returned by the Value property. (Overrides ValueTypeGetHashCode.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetTypeGets the Type of the current instance. (Inherited from Object.)
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetValueOrDefaultRetrieves the value of the current NullableT object, or the object's default value.
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps GetValueOrDefault(T)Retrieves the value of the current NullableT object, or the specified default value.
Public method Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps ToStringReturns the text representation of the value of the current NullableT object. (Overrides ValueTypeToString.)
Top
Operators
  NameDescription
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps (NullableT to T)Returns the value of a specified NullableT value.
Public operator Static member Supported by the XNA Framework Supported by Portable Class Library Supported in .NET for Windows Store apps (T to NullableT)Creates a new NullableT object initialized to a specified value.
Top
Remarks

A type is said to be nullable if it can be assigned a value or can be assigned , 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 .

The NullableT 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 NullableT 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 NullableT structure are the HasValue and Value properties. If the HasValue property for a NullableT 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 NullableT object, not the NullableT 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 NullableT structure initialized to the underlying value.

If the HasValue property of a nullable type is false, the result of a boxing operation is . 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 . When is unboxed into a nullable type, the common language runtime creates a new NullableT 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 NullableT type as a member of a structure exported in a WinMD library. Previously, this was not supported.

Examples

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.

' This code example demonstrates the Nullable(Of T) class. 
' The code example defines a database table in which two columns  
' are nullable. In the application, an array of rows is created  
' and initialized. The table rows could subsequently be  
' written to a database. 

Imports System

Class Sample
    ' Define the "titleAuthor" table of the Microsoft "pubs" database.  

    Public Structure titleAuthor
    ' Author ID; format ###-##-#### 
        Public au_id As String 
    ' Title ID; format AA#### 
        Public title_id As String 
    ' Author ORD is nullable. 
        Public au_ord As Nullable(Of Short)
    ' Royalty Percent is nullable. 
        Public royaltyper As Nullable(Of Integer)
    End Structure 'titleAuthor

    Public Shared Sub Main() 
    ' Declare and initialize the titleAuthor array. 
        Dim ta(2) As titleAuthor
        ta(0).au_id = "712-32-1176"
        ta(0).title_id = "PS3333"
        ta(0).au_ord = 1
        ta(0).royaltyper = 100

        ta(1).au_id = "213-46-8915"
        ta(1).title_id = "BU1032"
        ta(1).au_ord = Nothing
        ta(1).royaltyper = Nothing

        ta(2).au_id = "672-71-3249"
        ta(2).title_id = "TC7777"
        ta(2).au_ord = Nothing
        ta(2).royaltyper = 40

    ' Display the values of the titleAuthor array elements, and  
    ' display a legend.
        Display("Title Authors Table", ta)
        Console.WriteLine("Legend:")
        Console.WriteLine("An Author ORD of -1 means no value is defined.")
        Console.WriteLine("A Royalty % of 0 means no value is defined.")
    End Sub 'Main

    ' Display the values of the titleAuthor array elements. 
    Public Shared Sub Display(ByVal dspTitle As String, _
                              ByVal dspAllTitleAuthors() As titleAuthor) 
        Console.WriteLine("*** {0} ***", dspTitle)
        Dim dspTA As titleAuthor
        For Each dspTA In dspAllTitleAuthors
            Console.WriteLine("Author ID ... {0}", dspTA.au_id)
            Console.WriteLine("Title ID .... {0}", dspTA.title_id)
            Console.WriteLine("Author ORD .. {0}", dspTA.au_ord.GetValueOrDefault(-1))
            Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper.GetValueOrDefault(0))
            Console.WriteLine()
        Next dspTA
    End Sub 'Display
End Class 'Sample

' 
'This code example produces the following results: 
' 
'*** Title Authors Table *** 
'Author ID ... 712-32-1176 
'Title ID .... PS3333 
'Author ORD .. 1 
'Royalty % ... 100 
' 
'Author ID ... 213-46-8915 
'Title ID .... BU1032 
'Author ORD .. -1 
'Royalty % ... 0 
' 
'Author ID ... 672-71-3249 
'Title ID .... TC7777 
'Author ORD .. -1 
'Royalty % ... 40 
' 
'Legend: 
'An Author ORD of -1 means no value is defined. 
'A Royalty % of 0 means no value is defined. 
'
// This code example demonstrates the Nullable<T> class. 
// The code example defines a database table in which two columns  
// are nullable. In the application, an array of rows is created  
// and initialized. The table rows could subsequently be  
// written to a database. 

using System;

class Sample 
{
// Define the "titleAuthor" table of the Microsoft "pubs" database. 
    public struct titleAuthor 
    {
    // Author ID; format ###-##-#### 
    public string au_id;
    // Title ID; format AA#### 
    public string title_id;
    // Author ORD is nullable. 
    public short? au_ord;
    // Royalty Percent is nullable. 
    public int? royaltyper;
    }

    public static void Main() 
    {
// Declare and initialize the titleAuthor array.
    titleAuthor[] ta = new titleAuthor[3];
    ta[0].au_id = "712-32-1176";
    ta[0].title_id = "PS3333";
    ta[0].au_ord = 1;
    ta[0].royaltyper = 100;

    ta[1].au_id = "213-46-8915";
    ta[1].title_id = "BU1032";
    ta[1].au_ord = null;
    ta[1].royaltyper = null;

    ta[2].au_id = "672-71-3249";
    ta[2].title_id = "TC7777";
    ta[2].au_ord = null;
    ta[2].royaltyper = 40;

// Display the values of the titleAuthor array elements, and  
// display a legend.
    Display("Title Authors Table", ta);
    Console.WriteLine("Legend:");
    Console.WriteLine("An Author ORD of -1 means no value is defined.");
    Console.WriteLine("A Royalty % of 0 means no value is defined.");
    }

// Display the values of the titleAuthor array elements. 
    public static void Display(string dspTitle, 
                               titleAuthor[] dspAllTitleAuthors)
    {
    Console.WriteLine("*** {0} ***", dspTitle);
    foreach (titleAuthor dspTA in dspAllTitleAuthors)
       {
       Console.WriteLine("Author ID ... {0}", dspTA.au_id);
       Console.WriteLine("Title ID .... {0}", dspTA.title_id);
       Console.WriteLine("Author ORD .. {0}", dspTA.au_ord ?? -1);
       Console.WriteLine("Royalty % ... {0}", dspTA.royaltyper ?? 0);
       Console.WriteLine();       
       }
    }
}

/*
This code example produces the following results:

*** Title Authors Table ***
Author ID ... 712-32-1176
Title ID .... PS3333
Author ORD .. 1
Royalty % ... 100

Author ID ... 213-46-8915
Title ID .... BU1032
Author ORD .. -1
Royalty % ... 0

Author ID ... 672-71-3249
Title ID .... TC7777
Author ORD .. -1
Royalty % ... 40

Legend:
An Author ORD of -1 means no value is defined.
A Royalty % of 0 means no value is defined.

*/
Version Information

.NET Framework

Supported in: 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 Silverlight 8.1, Windows Phone Silverlight 8
Platforms

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.

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.