//
you're reading...
.NET 2.0

Weird feature in C# 2.0 and Nullable<T>

I just found this one while writing up some code for the XMPCR library.

Believe it or not, this is valid code:

private DateTime? turnOnDate;

This is apparently shorthand for using the new Nullable generic type so you can assign a database-style null value to a struct — in other words null means no value rather than no object.

Using Nullable<T> is also a bit easier under beta 1 than it was under the CTP because it provides transparent creation/usage of Nullable.

// old style - going from memory
Nullable<T> turnOnDate = new Nullable<T>( null );

// Beta 1 style
DateTime? turnOnDate = null;

Much better in my opinion.

Even better is that using it is pretty transparent. While this code is pretty obvious what it does, it is an over simplification of what i’m using elsewhere.

DateTime? foo = DateTime.Now;

TimeSpan? blah = DateTime.Now - ( foo + TimeSpan.FromMilliseconds( 500 ) );

Notice that I can use the operator overloads defined on the DateTime structure, in this case I am using two overloads. The + overload which returns a new DateTime instance that is the value of the timespan added to the DateTime. The second one, is the – overload which returns a TimeSpan instance which represents the amount of time between the two DateTime instances. Notice that the overall return value is a TimeSpan? (a Nullable<TimeSpan>), yet only one of the values in the equation on the right is Nullable, the foo variable. I’m not exactly sure what is going on there.

Running the above code under the debugger it works as expected. What is more interesting though is that if I change the code to:

DateTime? foo = null;

TimeSpan? blah = DateTime.Now - ( foo + TimeSpan.FromMilliseconds( 500 ) );

When I run this under the debugger blah is equal to null.

How does it all work? The secret lies in T Nullable<T>.GetValueOrDefault(); thanks to generics that method can return a valid default value if the instance is set to null. As an aside, that method is already marked with the obsolete attribute saying that you should use T Nullable.GetValueOrDefault<T>(T value);. I assume by the time the next beta comes out the compiler will be emitting the correct method to call.

It is important to note that Nullable is only useful when you use it with value types, because reference types can already be null there isn’t any advantage in using it and the GetValueOrDefault method would return an unusable value which wouldn’t help if you are using Nullable like I am in the last code snippet above; you would get a NullReferenceException while trying to perform the first addition.

End of ramble

Advertisements

About James

I am a Senior Developer/Consultant for InfoPlanIT, LLC. I previously spent over 7 years as a Product Manager for what eventually became ComponentOne, a division of GrapeCity. While there, I helped to create ActiveReports 7, GrapeCity ActiveAnalysis, and Data Dynamics Reports.

Discussion

2 thoughts on “Weird feature in C# 2.0 and Nullable<T>

  1. If you delcare it as:


    DateTime? dateTime;

    will it be no object? or a null value object?

    Posted by Nnamdi | July 13, 2004, 8:18 am
  2. It will default to being null. Good question though because the documentation doesn’t say much about it.

    Posted by James T. Johnson | July 13, 2004, 8:37 am

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

Archive

%d bloggers like this: