Pages

Men

rh

7/01/2012

Nullable Types in C#

Nullable Types:

  •  Nullable types are instances of the System. Nullable struct.
  •  A Nullable type can represent the normal range of values for its underlying value type, plus an additional null value.
  • Nullable types represent value-type variables that can be assigned the value of null. You cannot create a Nullable type based on a reference type. (Reference types already support the null value.)
  • The syntax T? is shorthand for System. Nullable<T>, where T is a value type. The two forms are interchangeable.
  • Assign a value to a Nullable type in the same way as for an ordinary value type, for example int? x = 10; or double? d = 4.108;
  • Use the HasValue and Value read-only properties to test for null and retrieve the value, for example if(x.HasValue) j = x.Value;
  • The HasValue property returns true if the variable contains a value, or false if it is null.
  • The Value property returns a value if one is assigned, otherwise a  System.InvalidOperationException is thrown.
  • The default value for a Nullable type variable sets HasValue to false. The Value is undefined.
  • Use the?? Operator to assign a default value that will be applied when a Nullable type whose current value is null is assigned to a non-Nullable type, for example int? x = null; int y = x ?? -1;
  • Nested Nullable types are not allowed. The following line will not compile: Nullable<Nullable<int>> n;
  • For example, a Nullable<Int32>, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable<bool> can be assigned the values true or false, or null.
Example:

Class NullableExample
{
    Static void Main ()
    {
        int? num = null;
        If (num.HasValue == true)
        {
            System.Console.WriteLine ("num = " + num.Value);
        }
        else
        {
            System.Console.WriteLine ("num = Null");
        }
        //y is set to zero
        Int y = num.GetValueOrDefault ();
// num.Value throws an InvalidOperationException if num.HasValue is false
        Try
        {
            y = num.Value;
        }
        Catch (System.InvalidOperationException e)
        {
            System.Console.WriteLine (e.Message);
        }
    }
}
 
The above will display the output:
num = Null
Nullable object must have a value.
Null coalescing operator (??):
The ?? Operator is called the null-coalescing operator and is used to define a default value for a nullable value types as well as reference types. It returns the left-hand operand if it is not null; otherwise it returns the right operand.

Example:
int b = a ?? -1;   
it means if the value of a is null then b=a otherwise the value of b will be -1.

Simply this means
If (a == null)    
     b = a; 
Else 
   b = -1; 
//The?? Operator also works with reference types:
    //message = param, unless param is null
    //in which case message = "No message"
    String message = param?? "No message";

To use the null-coalescing-operator, there are some compile-time ground rules.
  • The left-hand-side must evaluate to a reference or nullable type.
  • All evaluated statements must be of matching type, unless they can be implicitly converted.

No comments :

Post a Comment