Saturday, July 07, 2007

What is Nullable Type in .Net 2.0?

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 b = null; -OR-

// 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:

Anonymous said...

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.

Most Recent Post

Subscribe Blog via Email

Enter your email address:



Disclaimers:We have tried hard to provide accurate information, as a user, you agree that you bear sole responsibility for your own decisions to use any programs, documents, source code, tips, articles or any other information provided on this Blog.
Page copy protected against web site content infringement by Copyscape