How to identify a Nullable<T>

Before the recent Nullable<T> changes, some of you may have used INullableValue to find a Nullable<T>. We could debate the issues with writing code that uses a "marker" interface but here I want to just provide a solution for deciding if a type is Nullable<T>.

Now that the INullableValue interface is gone from Nullable<T>, I have got questions about how to identify a Nullable<T> type. Here is a way to do it:

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

 

Published 09 September 05 10:12 by Dinesh.Kulkarni

Comments

No Comments
New Comments to this post are disabled

About Dinesh.Kulkarni

I am a program manager in the Visual C# Product Unit of Microsoft. I am currently working on the LINQ project with specific responsibility for DLinq. Previously, I have been in a PM in SQL Server working on ObjectSpaces and DataSet. In pre-MS life, I have worked for companies ranging from startup to IBM on a wide range of software projects. Before I started working, I did M.S.E.E. and Ph.D. (CSE) from the University of Notre Dame and B.Tech. E.E. from IIT Bombay, India.

Search

This Blog

Syndication

Page view tracker