Nullable Types


Today I got an opportunity to try out the Nullable Types in the .NET Framework 2.0

We had a class inside an application I was working on which basically wrapped a DateTime value called SimpleDate and allowed the developer to treat it like an object.

 

public class SimpleDate
{
private DateTime simpleDateValue;
public SimpleDate()
{
}
public SimpleDate(DateTime newDate)
{
this.simpleDateValue = newDate;
}
public DateTime Value
{
get
{
return this.simpleDateValue;
}
set
{
this.simpleDateValue = value;
}
}
}

I was able to declare a nullable DateTime like so and replace the above class:

  • System.Nullable<DateTime> OR DateTime? in C#
  • Nullable(Of DateTime) in VB.NET

If you want to know if is a null or not you use the HasValue method to test your nullable type and you access its value using the Value method.

About wesmacdonald

Wes MacDonald is a Visual Studio ALM MVP, PSD, MCT and a Principal Consultant for LIKE 10 INC., a SharePoint Server, Visual Studio and Team Foundation Server Consulting Firm providing premium support and guidance services.

No comments yet... Be the first to leave a reply!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: