Identifying a Nullable Type Programmatically in Visual Studio

by demtron on Tuesday, December 16, 2008 05:34 PM

I have occasionally found a need to identify whether the type of a given variable is a nullable type so that I know whether I need to also use the HasValue and Value methods to obtain its contained value.  Here's a farily simple snippet of code to accomplish this.

VB.NET:
If type.IsGenericType AndAlso type.GetGenericTypeDefinition() = GetType(Nullable(Of )) Then
End If
C#:
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>)) {}

Powered by BlogEngine.NET 1.5.1.18
Theme by Mads Kristensen · Adapted by Demtron

Bookmark and Share

Calendar

<<  October 2024  >>
MoTuWeThFrSaSu
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910

View posts in large calendar
Log in

Milwaukee SEO Company

Milwaukee Access Programmer/Developer

Milwaukee Website Designer and Developer



Marketing / SEO

Blog Directory
blogarama - the blog directory
TopOfBlogs
Milwaukee area SEO, SEM, ASP.Net