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

<<  January 2025  >>
MoTuWeThFrSaSu
303112345
6789101112
13141516171819
20212223242526
272829303112
3456789

View posts in large calendar
Log in

Milwaukee SEO Company

Milwaukee Access Programmer/Developer

Milwaukee Website Designer and Developer

Subscribe rss feed!

Marketing / SEO

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