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

<<  March 2024  >>
MoTuWeThFrSaSu
26272829123
45678910
11121314151617
18192021222324
25262728293031
1234567

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