Using Nullable Types in C#
With the newest standard for the C# language, there is now support for nullable data types. This small change could be a huge help for those who deal with databases containing fields that are optional. Nullable data types can also be helpful in other situations as well.
In basic terms, a nullable data type is one that contain the defined data type or the value of null. The ECMA-334 standard for C# provides nullable versions of all the C# value types.
Defining Nullable Types
Defining a nullable type is very similar to defining the equivalent non-nullable type. The difference is in the use of the ? type modifier. To define an integer, you would normally do a simple declaration:
int myInt = 1;
To make myInt be able to store a null value, you would declare it as such:
int? myNullableInt = 1;
As you can see, these two variables look as though they are the same. The nullable version, however, is much different. The nullable version is actually a structure that combines a value type with a flag to indicate whether the value is null. Additionally, a nullable type has two publicly readable properties, HasValue and value. HasValue is a bool that is true if there is a value stored; otherwise, it is false if the variable is null. If HasValue is true, you can get the value of the variable. If it is false and you attempt to get the value, then an exception will be thrown.
null is now a keyword for C#. Additionally, it can be assigned to a nullable variable. The following are two valid assignments for a nullable variable:
double? myDouble = 3.14159;double? myOtherDouble = null;
As you can see, myDouble is assigned a value, but could also be assigned null. In the second statement, myOtherDouble is initialized to contain a null value something you can't do with a non-nullable type.
Using a Nullable Type
A nullable type can be used in the same way that a regular value type can be used. In fact, implicit conversions are built in for converting between a nullable and non-nullable variable of the same type. This means you can assign a standard integer to a nullable integer and vice-versa:
int? nFirst = null;int Second = 2;nFirst = Second; // ValidnFirst = 123; // ValidSecond = nFirst; // Also validnFirst = null; // ValidSecond = nFirst; // Exception, Second is nonnullable.
In looking at the above statements, you can see that a nullable and nonnullable variable can exchange values as long as the nullable variable does not contain a null. If it contains a null, an exception is thrown. To help avoid throwing an exception, you can use the nullable's HasValue property:
if (nFirst.HasValue) Second = nFirst;
As you can see, if nFirst has a value, the assignment will happen; otherwise, the assignment is skipped.
Using Operators with Nullable Values: Lifted Operators
In addition to the automatic conversions between a nullable and non-nullable variable of the same value type, there are also changes with the operators to allow them to work with nullable and non-nullable values. These operators are called lifted operators.
Consider the following code:
int ValA = 10;int? ValB = 3;int? ValC = ValA * ValB;
What is stored in Val C? The value of 30 would be stored into ValC. The standard operators have been modified so that they "lift" the non-nullable values to being nullable and thus allow the standard operations to work. Now, consider the following change:
int ValA = 10;int? ValB = null;int? ValC = ValA * ValB;
What would ValC contain this time? ValC would contain null. In the case where either operand is null, the result of a lifted operation will also be null. Even if you were doing addition or subtraction, it would still be null. So, ValA + ValB using the above values would result in null, not 10.
What if ValC were not a nullable type? What does the following do then?
int ValA = 10;int? ValB = null;int ValC = ValA * ValB; // ValC not nullable
This code would actually throw an exception. The result of ValA * ValB is null and a null can't be assigned to a non-nullable type. As such, an exception is thrown.
Page 1 of 2