.NET – Nullable data types
Software Development July 24th, 2006Lets face it…we’ve all been there. You’re pulling information out of a database and you’re getting frustrated because you have to set up all of these special cases to represent a null value. Well, with the .NET 2.0 framework your frustrations are a thing of the past. Enter the nullable data type. In the .NET 2.0 Framework, you can make any value type nullable. For review, a value type in .NET is one that stores its value directly, rather than just storing a reference to a value stored in memory. Common examples of value types are boolean, char, DateTime, and any numeric type such as integer, float, double, or decimal.
// Here is how a nullable integer can be declared in C# |
Nullable<int> myInt = null; |
// This is shorthand syntax for declaring a nullable integer in C# |
int? myInt = null; |
‘ This is how a nullable integer would be declared in VB |
Dim myInt As Nullable(Of Integer) = Nothing |
Now that we can declare a value type as nullable, how do we know if there is a value assigned or if the variable is null? If a value is set, how do we retrieve it? This is done easily by using the HasValue and Value members of a nullable type. Here is an example of how this is done.
// Using our variable myInt from above, this is how to extract the value in C# | |
if (myInt.HasValue) | |
Console.WriteLine(“The value of myInt is {0}.”, myInt.Value); | |
else | |
Console.WriteLine(“The value of myInt is null.”); | |
‘ This is how to accomplish the same task in VB |
|
If myInt.HasValue Then | |
Console.WriteLine(“The value of myInt is {0}.”, myInt.Value) | |
Else | |
Console.WriteLine(“The value if myInt is null.”) | |
End If |
Now you know the syntax for nullable types in .NET, you might be asking yourself what good are they? I’ll give you an example of how they might be used in a practical application, and you can use your imagination to come up with a way that they might be useful to you.
In my example, you are working on a project for a lab working with test results. Let’s say you have a database table called TestResults, and this table has several integer fields, Result1, Result2, Result3, and Result4. These fields can be null in the database, because all 4 tests aren’t necessarily run every time…so a NULL value would indicate that this particuliar test wasn’t run.
Now lets say that you have an object in .NET that represents a set of test results. If you used integers in your class to represent each of the 4 results, how would you express the fact that a test wasn’t run? You could use 0, or -1, but what if those were valid test results? You could pick an integer value that you know is not a valid result to indicate this……but will that make sense to you in 6 months when you have to make some changes to this software? You could also use strings in your class to store test results, but that gets clunky if you actually want to do something with these results.
Making the fields nullable integers would be an ideal solution in this case. You have access to the results as integers, and you can match the database logic and store NULL values when the tests weren’t run. This will also make as much sense to you in 6 months as it did the day you wrote it, because you wouldn’t be dealing with hard coded constants to indicate tests that weren’t run, etc. You also, at least in my opinion, get code that is much cleaner and easier to maintain.
The ability to add nullable value types in the .NET framework is a very nice addition in my opinion…I hope that this article has at least given you an idea of the possibilities.
July 25th, 2006 at 2:15 pm
A good overview of one of the great new toys from .NET 2.0. I’m constantly finding new uses for nullable types.
July 25th, 2006 at 2:58 pm
Nice intro. I think you have a typo in your first example. Shouldn’t
// Here is how a nullable integer can be declared in C#
Nullable myInt = null;
be
Nullable myInt = null;
July 25th, 2006 at 3:16 pm
At first I thought you were smoking something, but then I realized what you meant — the generics braces are missing, right? It should be Nullable<type>. I guess I completely missed that when I read the article. Funny how that works sometimes…
July 25th, 2006 at 3:43 pm
Good catch! WordPress ate part of that line and I didn’t notice it. Looks like the same thing happened to your post, which had me scratching my head for a few minutes.
Fixed now.
August 26th, 2007 at 3:25 pm
Thank you for your site. I have found here much useful information.
Good site !
February 6th, 2008 at 9:55 am
Great post, thank you for putting it together.
I am trying to get a better handle on this concept as I have never had a chance to use this myself.