What is a nullable type?
Nullable types help us circumvent the problem on not being able to set a value type to a null value, after it has been initialized. To understand why value types can not be assigned a null value, I recommend reading about .NET Stack and Heap memory.
Value types are values that reside in private stack, including:
- (all integers)
- (all floating-point numbers)
- char
- bool
Nullable types are types that are able to contain a null value. All value types have a nullable version.
How nullable types work
A nullable type really isn't a type in itself. These are simply implementations of the Nullable
Syntax & Evaluation
Because a nullable type is a struct, we can not (should not) evaluate their value as one would a value type. We must call the Value property, to retrieve it. (Fortunately, .NET accommodates programmers not aware of this need, by providing a mechanism allowing normal value type syntax, given the value is not null.)_
To declare a type as nullable, simply post-fix the type name with the " ? " character. For example:
// Declare a nullable type int? nullableType = 356; nullableType = null;If we replace int? with just regular int, the above code will not compile. As mentioned earlier, the nullable type value must be retrieved by way of the Value property:
// Declare a nullable type
int? nullableType = 356;
nullableType = null;
// Declare a value type -- this can not be set to null!
int valueType;
// This line will cause a runtime error, because the returned
// value is null. Value types can not be set to null!
value type = nullableTypeInt; 
// We may determine if the nullable type contains  a null
// value, and then make an assignment.  Also, we will
// retrieve the value by calling the Value property.
if (!nullableType.HasValue)
{
    valueType = nullableType.Value;
}
// We can set a default value, when the nullable
// type has no (null) value.
const int defaultValue = -1;
if (!nullableTypeInt.HasValue)
{
    valueType = nullableType.Value;
}
else
{
    valueType = defaultValue;
}
// Nullable<t> provides the Null Coalescing Operator (??),
// to reduce the amount of code needed to achieve
// the same results as above.  This works similar
// to the ternary (?:) operator.
valueType = nullableType ?? defaultValue;
Efficient Use of Nullable Types
There are only a few basic rules to efficient use of nullable types, all of which you probably already know or have guessed:
- Use nullable types only when needed
- Check for null by calling the HasValue property, before attempting to use the value
- Retrieve the value by calling the Value property
- Use the ?? operator, to save space and time
 
 
No comments:
Post a Comment
Please provide details, when posting technical comments. If you find an error in sample code or have found bad information/misinformation in a post, please e-mail me details, so I can make corrections as quickly as possible.