Tuesday 4 August 2015

What is Nullable in.Net and how to use?

C# provided a special data type called as Nullable type. In Nullable type you can assign a normal range values i.e  -2,147,483,648 to 2,147,483,647 or null value also.

In int,string,float,double etc data type we can not assign null values.

int intNo = null; // compiler error : Cannot convert null to int because it is non-nullable value type

Syntax:

<data_type>?  <variable-name> = null;

Example :-

static void Main(string[] args)
{
   int? num = null;
   int? num = 45;
   string strWord = null;
   double? d = 3.478;
   object o = null;
   Console.WriteLine("{0},{1},{2},{3},{4}",num,num1,strWord,d,o);
   Console.ReadLine();
}


Output:   , 45 ,  ,3.478,

No comments:

Post a Comment

Note: only a member of this blog may post a comment.