More About Generics - 4.6 Creating a Value Type That Can Be Initialized to Null
(Page 2 of 4 )
Problem
You have a variable that is a numeric type, which will hold a numeric value obtained from a database. The database may return this value as a null. You need a simple, clean way to store this numeric value, even if it is returned as a null.
Solution
Use a nullable value type. There are two ways of creating a nullable value type. The first way is to use the ? type modifier:
int? myDBInt = null;
The second way is to use theNullable<T>generic type:
Nullable<int> myDBInt = new Nullable<int>();
Discussion
Both of the following statements are equivalent:
int? myDBInt = null;
Nullable<int> myDBInt = new Nullable<int>();
In both cases,myDBIntis a nullable type and is initialized tonull.
A nullable type implements theINullableValueinterface, which has two read-only property members,HasValueandValue. TheHasValueproperty returnsfalseif the nullable value is set tonull; otherwise, it returnstrue. IfHasValuereturnstrue, you can access theValue property, which contains the currently stored value. IfHasValue returnsfalseand you attempt to read theValueproperty, you will get anInvalidOperationExceptionthrown. This is because theValueproperty is undefined at this point. Below is an example of a test of nullable value using theHasValue property value:
if (myDBInt.HasValue)
Console.WriteLine("Has a value: " + myDBInt.Value);
else
Console.WriteLine("Does not have a value (NULL)");
In addition, one can simply compare the value tonull, as shown below:
if (myDBInt != null)
Console.WriteLine("Has a value: " + myDBInt.Value);
else
Console.WriteLine("Does not have a value (NULL)");
Either method is acceptable.
When casting a nullable value to a non-nullable value, the cast operates as it would normally, except when the nullable type is set tonull. In this case, anInvalidOperationExceptionis thrown. When casting a non-nullable value to a nullable value, the cast operates as it would normally. NoInvalidOperationExceptionwill be thrown, as the non-nullable value can never benull.
The tricky thing to watch out for with nullable types is when comparisons are performed. For example, if the following code is executed:
if (myTempDBInt < 100)
Console.WriteLine("myTempDBInt < 100");
else
Console.WriteLine("myTempDBInt >= 100");
The text “myTempDBInt >= 100” is displayed, which is obviously incorrect if the value ofmyTempDBInt isnull. To fix this code, you have to check ifmyTempDBIntisnull. If it is not, you can execute theifstatement in the previous code block:
if (myTempDBInt != null)
{
if (myTempDBInt < 100)
Console.WriteLine("myTempDBInt < 100");
else
Console.WriteLine("myTempDBInt >= 100");
}
else
{
// Handle the null here.
}
Another interesting thing about nullable types is that you can use them in expressions similar to normal numeric types, for example:
int? DBInt = 10;
int Value = 2;
int? Result = DBInt + Value; // Result == 12
The result of using a nullable value in most operators is anull if any nullable value isnull.
Neither the comparison operators nor the null coalescing operator lift to nullable.
However, if none of the nullable values isnull, the operation is evaluated as it normally would be. IfDBInt, for example, were set tonull, the value placed inResult would also benull.
See Also
The “Nullable<T>Generic Class” and “Using Nullable Types” topics in the MSDN documentation.
Next: 4.7 Reversing the Contents of a Sorted List >>
More C# Articles
More By O'Reilly Media
|
This article is excerpted from chapter four of the C# 3.0 Cookbook, Third Edition, written by Jay Hilyard and Stephen Teilhet (O'Reilly, 2008; ISBN: 059651610X). Check it out today at your favorite bookstore. Buy this book now.
|
|