Nullable Type in C#

Nullable type in C#
We can declare a variable as nullable when we want to know that a value has been assigned to that variable or not. Declaring a variable as nullable enables the HasValue and Value members. We can use the HasValue property on the variable to check if the value has been assigned to the variable.

Example of Using Nullable
Let’s take an example of boolean variable. As we know the default value for a boolean variable is false. But we want that we should be able to know if user has selected true or false. So we define the bool variable as nullable like this:

Nullable<bool> testVariable= null;

Now we have the testVariable as a boolean variable having a value of null. Now when use the properties like this :

if(testVariable.HasValue)
MessageBox.Show(String.Format("Value is {0}",testVariable.value )); 

kick it on DotNetKicks.com

Shout it

pimp it

Anky Goyal

I am MCPD certified in .NET and working on .NET technologies for past 3yrs. I am very passionate about the new features which get introduced in the technology. I am here to share the new features i get to know in the world of Microsoft. Follow me on twitter @ankygoyal LinkedIN@Ankit Goyal

More Posts - Website

2 Responses to Nullable Type in C#


  1. acid reflux home cure
    Apr 29, 2011

    Greetings everyone, This webpage is excellent and so is how the matter was expanded. I like a number of the comments as nicely although I would favor all of us maintain it on matter in order add worth to the subject. It will be additionally encouraging to the creator if all of us might share it (for some of us who use bookmarking companies such as a digg, twitter,..).

Trackbacks/Pingbacks

  1. DotNetShoutout

Leave a Reply