Nullable Types in C#


The primitive Data Types in C# (like int,double,float,bool etc) will not accept the null values.To make these primitive data types accept null values we will use the Nullable Types.Nullable data types can represent all the values of an underlying type plus an additional null value.
When there are hundreds of values for a perticular column in a database table,there are chances that some of the values might have null in it.In those scenarios ,instead of checking against null value ,we can directly assign into the primitive data types.
Nullable types are instances of the struct System.Nullable<T> where T can hold any primitive data type. Nullable types can be declared in two different ways
    1)Nullable<int> i = null;
    2)int? j= 10;
 
    The nullable type in C# has two properties namely HasValue and Value.
    HasValue :  If the nullable type variable holds a definite value then this property returns True. If the nullable type variable holds null value then HasValue property returns False.
    Value:  It returns value of the nullable type.

No comments:

Post a Comment