Nullable.GetUnderlyingType Method (System)

Switch View :
ScriptFree
.NET Framework Class Library
Nullable.GetUnderlyingType Method

Returns the underlying type argument of the specified nullable type.

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

Visual Basic
Public Shared Function GetUnderlyingType ( _
	nullableType As Type _
) As Type
C#
public static Type GetUnderlyingType(
	Type nullableType
)
Visual C++
public:
static Type^ GetUnderlyingType(
	Type^ nullableType
)
F#
static member GetUnderlyingType : 
        nullableType:Type -> Type 

Parameters

nullableType
Type: System.Type
A Type object that describes a closed generic nullable type.

Return Value

Type: System.Type
The type argument of the nullableType parameter, if the nullableType parameter is a closed generic nullable type; otherwise, null.
Exceptions

Exception Condition
ArgumentNullException

nullableType is null.

Remarks

A generic type definition is a type declaration, such as Nullable<T>, that contains a type parameter list, and the type parameter list declares one or more type parameters. A closed generic type is a type declaration where a particular type is specified for a type parameter.

For example, if the nullableType parameter is the type of Nullable<Int32> in C# (Nullable(Of Int32) in Visual Basic), the return value is the type of Int32 (that is, the type argument of the closed generic type).

Examples

The following code example defines a method whose return value is of type Nullable<T> of Int32. The code example uses the GetUnderlyingType method to display the type argument of the return value.

Visual Basic

' This code example demonstrates the 
' Nullable.GetUnderlyingType() method.

Imports System
Imports System.Reflection

Class Sample
    ' Declare a type named Example. 
    ' The MyMethod member of Example returns a Nullable of Int32.

    Public Class Example
        Public Function MyMethod() As Nullable(Of Integer)
            Return 0
        End Function
    End Class 'Example

' 
'   Use reflection to obtain a Type object for the Example type.
'   Use the Type object to obtain a MethodInfo object for the MyMethod method.
'   Use the MethodInfo object to obtain the type of the return value of 
'     MyMethod, which is Nullable of Int32.
'   Use the GetUnderlyingType method to obtain the type argument of the 
'     return value type, which is Int32.
'
    Public Shared Sub Main() 
        Dim t As Type = GetType(Example)
        Dim mi As MethodInfo = t.GetMethod("MyMethod")
        Dim retval As Type = mi.ReturnType
        Console.WriteLine("Return value type ... {0}", retval)
        Dim answer As Type = Nullable.GetUnderlyingType(retval)
        Console.WriteLine("Underlying type ..... {0}", answer)

    End Sub 'Main
End Class 'Sample
'
'This code example produces the following results:
'
'Return value type ... System.Nullable`1[System.Int32]
'Underlying type ..... System.Int32
'


C#

// This code example demonstrates the 
// Nullable.GetUnderlyingType() method.

using System;
using System.Reflection;

class Sample 
{
// Declare a type named Example. 
// The MyMethod member of Example returns a Nullable of Int32.

    public class Example
    {
        public int? MyMethod() 
        {
        return 0;
        }
    }

/* 
   Use reflection to obtain a Type object for the Example type.
   Use the Type object to obtain a MethodInfo object for the MyMethod method.
   Use the MethodInfo object to obtain the type of the return value of 
     MyMethod, which is Nullable of Int32.
   Use the GetUnderlyingType method to obtain the type argument of the 
     return value type, which is Int32.
*/   
    public static void Main() 
    {
        Type t = typeof(Example);
        MethodInfo mi = t.GetMethod("MyMethod");
        Type retval = mi.ReturnType;
        Console.WriteLine("Return value type ... {0}", retval);
        Type answer = Nullable.GetUnderlyingType(retval);
        Console.WriteLine("Underlying type ..... {0}", answer);
    }
}
/*
This code example produces the following results:

Return value type ... System.Nullable`1[System.Int32]
Underlying type ..... System.Int32

*/


Version Information

.NET Framework

Supported in: 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
Platforms

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.
See Also

Reference