Wednesday, June 20

C# Nullable Types

When you declare a variable, you ask the compiler to reserve a certain area and amount of memory to eventually hold a value for that variable. At that time, no clear value is stored in that reserved area of memory. Instead, the area gets filled with garbage, which value is referred to as null when it cannot be clearly determined. A null value is not 0 because 0 is an actual value.

 In C#, normally, not all variables can hold null values. But if you want to do so you can do it.

There is a tow way of declaring Nullable variables, in here I will mention both ways.   
  

System.Nullable<T> variableName
-Or-
T? variableName 

T is the underlying type of the Nullable type. T can be any value type including struct. It cannot be a reference type.

E.g.:  int? i = null;
  
When you create a Nullable of Int32, it can be assigned any value from -2147483648 to 2147483647 or it can be assigned the null value. 

The Nullable type modifier enables C# to create value-type variables that indicate an undefined value. 
 

No comments:

Post a Comment