BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

C# Programming
.NET 2.0+

C# Nullable Numeric Data Types

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.

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

Value Property

The numeric nullable data types include a second property, named Value, that can be used to retrieve the value from a variable as a non-nullable type. This provides the same effect as casting from a nullable type to its non-nullable counterpart. As with casting, a run-time error will occur should the value of the variable be null. The previous example can therefore also be written as follows:

int? a = 10;
int? n = null;
int result;
bool checkIfNull;

checkIfNull = a.HasValue;               // checkIfNull = true
result = checkIfNull ? a.Value : -1;    // result = 10

checkIfNull = n.HasValue;               // checkIfNull = false
result = checkIfNull ? n.Value : -1;    // result = -1

result = n.Value;                       // This causes a run-time error.

GetValueOrDefault Method

The GetValueOrDefault method is available to all of the numeric nullable data types. It provides all of the functionality of the previous example in a single statement. The method can be called in two ways. If used without a parameter the numeric value of the nullable data is returned. If the variable in question is null, zero is returned instead. The second way to call the method uses a parameter to specify the value to replace nulls with. As with all methods, the parameter is held in parentheses. Use an empty pair of parentheses to pass no parameters.

int? a = 10;
int? n = null;
int result;

result = a.GetValueOrDefault();         // result = 10

result = n.GetValueOrDefault();         // result = 0
result = n.GetValueOrDefault(-1);       // result = -1

The Null Coalescing Operator

The null coalescing operator checks if the value of a variable is null. If the value is not null its value is returned. If it is null, a substitute value, provided as a second operand, is returned. The operator provides similar functionality to the GetValueOrDefault method with the benefit that it can be used on types that do not provide this functionality. The operator's symbol is a double question mark (??).

int? a = 10;
int? n = null;
int result;

result = a ?? -1;     // result = 10
result = n ?? -1;     // result = -1

The addition of the null coalescing operator completes the list of operators that are included in the C# Fundamentals tutorial. The next article in the tutorial provides a tabular reference to C# operator precedence.

1 October 2006