You are on page 1of 4

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

http://msdn.microsoft.com/en-us/library/ms366789

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


Visual Studio 2010 3 out of 4 rated this helpful - Rate this topic 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 Nullable Types (C# Programming Guide) Boxing Nullable Types (C# Programming Guide)

1 of 4

5/25/2012 11:48 AM

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

http://msdn.microsoft.com/en-us/library/ms366789

Did you find this helpful?

Yes

No

Community Content
Simple trick to get Underlying nullable value if it exists
Given some type T, here's a simple way to always have the real (underlying type), regardless if it is nullable or not: var realType = Nullable.GetUnderlyingType(typeof(T)) ?? typeof(T);

4/6/2012 Thomas Lee

3/30/2012 Wuzi

Corollary: determining whether an object has a Nullable type


In addition to determining whether a Type is nullable, it's also useful to see whether an object is of a Type which is nullable. That's easier said than done. Presumably, one would just call Object.GetType() and determine the nullability of the returned Type, right? Wrong. Attempting to directly retrieve the Type of a nullable object using GetType() will automatically box the object, no longer accessing the original object but rather the non-nullable boxed object. Thus, 'mynullableobject.GetType()' will always return false. Here's a reliable solution (see CAVEAT below). Refer to my full solution, explanation, and examples at http://stackoverflow.com/questions/374651/how-to-check-if-an-object-is-nullable /7761924#7761924 for further detail. public static bool IsObjectNullable<T>(T obj) { // If the parameter-Type is a reference type, or if the parameter is null, then the object is always nullable if (!typeof(T).IsValueType || obj == null) return true; // Since the object passed is a ValueType and is not null, it cannot be a nullable object return false;

public static bool IsObjectNullable<T>(T? obj) where T : struct { // Always return true, since the object-type passed is guaranteed by the compiler to always be nullable return true; }

2 of 4

5/25/2012 11:48 AM

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

http://msdn.microsoft.com/en-us/library/ms366789

CAVEAT: This method works reliably only if called using the original object reference or an exact copy, as shown in the linked examples. However, if a nullable object is boxed to another Type (such as object, etc.) instead of remaining in its original Nullable<> form, this method will not work reliably. If the code calling this method is not using the original, unboxed object reference or an exact copy, it cannot reliably determine the object's nullability using this method. In most coding scenarios, to determine nullability one must instead rely on testing the original object's Type, not its reference (e.g., code must have access to the object's original Type to determine nullability). In these more common cases, IsTypeNullable is a reliable method of determining nullability (see link below). See the above link for further detail and examples. 10/14/2011 wmarkjones 10/14/2011 wmarkjones

Prologue: What is the practical meaning of nullability here?


I should repeat a statement about nullability I made in a separate post, which applies directly to properly addressing this topic. That is, I believe the focus of the discussion here should not be how to check to see if an object is a generic Nullable<T> type, but rather whether one can assign a value of null to an object of its type. In other words, I think we should be determining whether an object type is nullable, not whether it is Nullable<T>. The difference is in semantics, namely the practical reasons for determining nullability, which is usually all that matters. In a system using objects with types possibly unknown until run-time (web services, remote calls, databases, feeds, etc.), a common requirement is to determine whether a null can be assigned to the object, or whether the object might contain a null. Performing such operations on non-nullable types will likely produce errors, usually exceptions, which are very expensive both in terms of performance and coding requirements. To take the highly-preferred approach of proactively avoiding such problems, it is necessary to determine whether an object of an arbitrary Type is capable of containing a null; i.e., whether it is generally 'nullable'. In a very practical and typical sense, nullability in .NET terms does not at all necessarily imply that an object's Type is a form of Nullable<T>. In many cases in fact, objects have reference types, can contain a null value, and thus are all nullable; none of these have a Nullable<T> type. Therefore, for practical purposes in most scenarios, testing should be done for the general concept of nullability, vs. the implementation-dependent concept of Nullable<T>. So we should not be hung up by focusing solely on the .NET Nullable<T> type but rather incorporate our understanding of its requirements and behavior in the process of focusing on the general, practical concept of nullability. 10/14/2011 wmarkjones

Determining whether a Type is nullable and converting a Type to a nullable type


Here is my solution: http://stackoverflow.com/questions/108104/how-do-i-convert-a-system-type-toits-nullable-version/7759487#7759487 10/14/2011 wmarkjones

3 of 4

5/25/2012 11:48 AM

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

http://msdn.microsoft.com/en-us/library/ms366789

GetType() throw exception when the value is null


static void Main(string[] args) { int? x = null; Type type = x.GetType(); //x si null and throw exception if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) { Console.WriteLine("It's nullable datat type"); } } 10/12/2011 Jozef Franc

2012 Microsoft. All rights reserved.

4 of 4

5/25/2012 11:48 AM

You might also like