Types that are value types but can hold a null value also apart from the normal range of integer values e.g. Nullable, pronounced "Nullable of Int32," can be assigned any value from -2147483648 to 2147483647, or it can be assigned the null value. A Nullable can be assigned the values true or false, or null.
Nullable types are instances of the System.Nullable struct.
Declaration:- int? i or Nullable i;
Examples:- In example below HasValue returns true if object is not null. int? num = null; if (num.HasValue == true) { System.Console.WriteLine("num = " + num.Value); } else { System.Console.WriteLine("num = Null"); }
To give default value or null use int y = num.GetValueOrDefault();
Besides this the Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown
Prior to .Net 2.0 there was a huge gap between database types and c# types e.g. for an int value we can have null in database but in c# int or we can say value types couldn't hold null values..Net 2.0 was the first step towards bridging this gap and it did by introducing nullable types i.e.
|
No responses found. Be the first to respond and make money from revenue sharing program.
|