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<>)) {}

Comments

Comments are closed

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

Bookmark and Share

Calendar

<<  December 2024  >>
MoTuWeThFrSaSu
2526272829301
2345678
9101112131415
16171819202122
23242526272829
303112345

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