Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Visual C#
 How to: Identify a Nullable Type
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
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
      //…
  }

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<>)) {…}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
What?????      Diego Colombo   |   Edit   |  
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 What's this?: Add a tag
Flag as ContentBug
How to get type?      Thomas Appel   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
The answer to the above: You can't      R1ck   |   Edit   |  
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 What's this?: Add a tag
Flag as ContentBug
Another example      ausrob   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
Example using inferred TypeParam      Jim Blythe   |   Edit   |  

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 What's this?: Add a tag
Flag as ContentBug
Processing
© 2008 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker