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.

Wes MacDonald's avatar

About Wes MacDonald

Wes MacDonald is a DevOps Consultant for LIKE 10 INC., a DevOps consulting firm providing premium support, guidance and services for Azure, Microsoft 365 and Azure DevOps.

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

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.