What is Nullable Type in .Net 2.0?
Nullable in .Net 2.0, helps to determine whether variable has been assigned a value or not. Example: Quiz application having option yes/no, but it should displayed “Not Attempted” when user does not make any choice.
Declaring a variable as nullable enables the HasValue and Value members.
Nullable
// Shorthand notation for declaring nullable type, only for C#
bool? b = null;
Example of Nullable Type
Use HasValue to detect whether or not a value has been set:
if (b.HasValue)
Console.WriteLine("User has Attempted Given Question");
else
Console.WriteLine("User has not Attempted Given Question");
//Output
User has not Attempted Given Question
The Nullable type is a new feature in .NET 2.0.
1 comment:
I want to add some more in this useful blog :-
Nullable types have 2 read-only properties hasvalue which returns whether the instance is null or not and value which returns the value where hasvalue is true.
Post a Comment