
.NET 2.0+C# Nullable Numeric Data Types (2)
The thirteenth part of the C# Fundamentals tutorial reviews the C# numeric data types. In this article we investigate the concept of undefined or nullable data and the data types introduced as part of .NET 2.0 that permit the storage of null information.
Arithmetic Operators
The standard arithmetic operators can be used with nullable types. If the value of any of the operands is null, the result will be null regardless of the other values.
int? a = 55;
int? n = null;
int? result;
result = a * 2; // result = 110
result = a * n; // result = null
Boolean Operators
When using nullable Booleans, the standard binary Boolean logical operators can be used. Where both of the operands used are either true or false, the results of the operation are the same as for non-nullable Booleans. Where one or both of the operands are set to null, the result is usually null. There are two special cases where this does not happen. In a logical OR operation, if any value is true the result is true, even if the other operand is null. For logical AND operations, if either value is false the result is false.
bool? result;
result = true & null; // result = null
result = false & null; // result = false
result = true | null; // result = true
result = false | null; // result = null
result = true ^ null; // result = null
result = false ^ null; // result = null
Relational Operators
The relational operators are all valid for use with nullable numeric types. When the value being compared is null, the results are not always as expected. The equal to and not equal to operators are able to compare both numeric and null values. With all of the other relational operators, the result of the comparison is false when a value being compared is null.
int? a = 55;
int? n = null;
bool result;
result = a == n; // result = false
result = a != n; // result = true
result = n == null; // result = true
result = a > n; // result = false
result = a < n; // result = false
Testing for Null Values
The previous section showed the use of relational operators with nullable types. In the examples you can see that it is possible to use the equal to or not equal to operators to test if the value of a variable is null. In addition, the nullable data types define several properties and methods for checking if the value is null and for retrieving the value where it is not.
HasValue Property
The first property of interest is the HasValue property. This returns a Boolean value indicating whether the nullable variable contains a value or null. To access the value of a property, the member access operator is used. This is a full stop (period or dot) placed between the name of the variable and the name of the member (property or method) to be used. The following example shows the HasValue property used to set a non-nullable value to the value of a nullable type with a default value of -1 when the nullable variable has no value.
int? a = 10;
int? n = null;
int result;
bool checkIfNull;
checkIfNull = a.HasValue; // checkIfNull = true
result = checkIfNull ? (int)a : -1; // result = 10
checkIfNull = n.HasValue; // checkIfNull = false
result = checkIfNull ? (int)n : -1; // result = -1
1 October 2006