C# Programming Guide
How to: Identify a Nullable Type (C# Programming Guide)

You can use the C# typeof operator to create a Type object that represents a Nullable type:

System.Type type = typeof(int?);

You can also use the classes and methods of the System.Reflection namespace to generate Type objects that represent Nullable types. However, if you try to obtain type information from Nullable variables at runtime by using the GetType method or the is operator, the result is a Type object that represents the underlying type, not the Nullable type itself.

Calling GetType on a Nullable type causes a boxing operation to be performed when the type is implicitly converted to Object. Therefore GetType always returns a Type object that represents the underlying type, not the Nullable type.

  int? i = 5;
  Type t = i.GetType();
  Console.WriteLine(t.FullName); //"System.Int32"

The C# is operator also operates on a Nullable's underlying type. Therefore you cannot use is to determine whether a variable is a Nullable type. The following example shows that the is operator treats a Nullable<int> variable as an int.

  static void Main(string[] args)
  {
    int? i = 5;
    if (i is int) // true
      //…
  }
Example

Use the following code to determine whether a Type object represents a Nullable type. Remember that this code always returns false if the Type object was returned from a call to GetType, as explained earlier in this topic.

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}
See Also

Reference

Tags :


Community Content

Diego Colombo
What?????
If the code is :
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}

but :

if the Type object was returned from a call to GetType, as explained earlier in this topic.

HOW DO I GET THE TYPE OBJECT?????



Tags :

Thomas Appel
How to get type?

Like Diego I am wondering how to get the type of an object.

E.g.

int? a = new int?(42);

System.Type type = a.SomeMagicFunctionOtherThanGetType();

And then I can use the above mentioned if statement:

if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {…}

Tags :

R1ck
The answer to the above: You can't
The reason is simple: if you cast to an object, the information that it was once nullable is gone forever. Think about it for a while, it's quite logical.

The only valid reason for wanting to know it is nullable is if you have a generic method. Then the solution is to use typeof(T) (C#) or GetType(T) (VB)

where T is your generic type parameter.

Tags :

ausrob
Another example

Common scenario might be if you are enumerating the properties of a class (e.g. called MyClass) using reflection.

PropertyInfo[] props = typeof(MyClass).GetProperties();

foreach(PropertyInfo p in props)

{

Type t = p.PropertyType;

//check if generic

if (t.IsGenericType && t.GetGenericTypeDefinition() == typeof(Nullable<>))

{

}

}

Tags :

Jim Blythe
Example using inferred TypeParam

Using an inferred TypeParam, you can get the type as a variable:

///<summary>Identify a Nullable Type</summary>
public static bool NullableType<T>(T value)
{
    var type = typeof(T);
    return (type.IsGenericType && type.GetGenericTypeDefinition() == typeof (Nullable<>));
}
Tags :

ap3rus
Extracting type from Nullable
///<summary>Indentify and extracting type from Nullable Type</summary>
public static Type ExtractTypeFromNullable(Type type)
{
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
{
var valueProp = type.GetProperty("Value");
return valueProp.PropertyType;
}
else
{
return null;
}
}

Page view tracker