- Partial Types
- Generics
- Nullable Value Types
- The Lightweight Transaction Manager
- Role Providers
- Summary
- References
Nullable Value Types
According to the Common Language Infrastructure specification, there are two ways of representing data in .NET: by a value type or by a reference type (Ecma 2006, 18). Although instances of value types are usually allocated on a thread’s stack, instances of reference types are allocated from the managed heap, and their values are the addresses of the allocated memory (Richter 2002, 134–5).
Whereas the default value of a reference type variable is null, indicating that it has yet to be assigned the address of any allocated memory, a value type variable always has a value of the type in question and can never have the value null. Therefore, although you can determine whether a reference type has been initialized by checking whether its value is null, you cannot do the same for a value type.
However, there are two common circumstances in which you would like to know whether a value has been assigned to an instance of a value type. The first is when the instance represents a value in a database. In such a case, you would like to be able to examine the instance to ascertain whether a value is indeed present in the database. The other circumstance, which is more pertinent to the subject matter of this book, is when the instance represents a data item received from some remote source. Again, you would like to determine from the instance whether a value for that data item was received.
The .NET Framework 2.0 incorporates a generic type definition that provides for cases like these in which you want to assign null to an instance of a value type, and test whether the value of the instance is null. That generic type definition is System.Nullable<T>, which constrains the generic type arguments that may be substituted for T to value types. Instances of types constructed from System.Nullable<T> can be assigned a value of null; indeed, their values are null by default. Thus, types constructed from System.Nullable<T> are referred to as nullable value types.
System.Nullable<T> has a property, Value, by which the value assigned to an instance of a type constructed from it can be obtained if the value of the instance is not null. Therefore, you can write
System.Nullable<int> myNullableInteger = null; myNullableInteger = 1; if (myNullableInteger != null) { Console.WriteLine(myNullableInteger.Value); }
The C# programming language provides an abbreviated syntax for declaring types constructed from System.Nullable<T>. That syntax allows you to abbreviate
System.Nullable<int> myNullableInteger;
to
int? myNullableInteger;
The compiler will prevent you from attempting to assign the value of a nullable value type to an ordinary value type in this way:
int? myNullableInteger = null; int myInteger = myNullableInteger;
It prevents you from doing so because the nullable value type could have the value null, which it actually would have in this case, and that value cannot be assigned to an ordinary value type. Although the compiler would permit this code,
int? myNullableInteger = null; int myInteger = myNullableInteger.Value;
the second statement would cause an exception to be thrown because any attempt to access the System.Nullable<T>.Value property is an invalid operation if the type constructed from System.Nullable<T> has not been assigned a valid value of T, which has not happened in this case.
One proper way to assign the value of a nullable value type to an ordinary value type is to use the System.Nullable<T>.HasValue property to ascertain whether a valid value of T has been assigned to the nullable value type:
int? myNullableInteger = null; if (myNullableInteger.HasValue) { int myInteger = myNullableInteger.Value; }
Another option is to use this syntax:
int? myNullableInteger = null; int myInteger = myNullableInteger ?? -1;
by which the ordinary integer myInteger is assigned the value of the nullable integer myNullableInteger if the latter has been assigned a valid integer value; otherwise, myInteger is assigned the value of -1.