Thursday, October 29, 2009

Nullable in c#

Nullable is used for assigning null value to a variable. For example we have a variable for finding user's selection of Gender . If the user doesn't select any option we should get an information .Here comes the advantage of using nullable. There are two ways to use nullable option in c#.

1) bool? blnVar =null;


if( b.HasValue )
{
Console.WriteLine(b.value);
}
else
{
Console.WriteLine("Value not set for b");
}
2) Nullable blnVar=null;
in this manner also we can obtain the HasValue