CODEDIGEST
Home » Articles
Search
 

Technologies
 

CodeDigest Navigation
 

Technology News
No News Feeds available at this time.
 

Community News
No News Feeds available at this time.
 
Nullable Types in C#

By Satheesh babu
Posted On Feb 18,2008
Article Rating:
Average Rating: 4
No of Ratings: 1
No of Comments: 2
Category:
Print this article.

Nullable Types in C#

 

In 1.x days we cannot assign a null value to value types.Assigning a null value will throw an error.

 

int mobile = null; //Will throw an error

 

This is an inconvieneince whenever we are retrieving a value from a database that can allow nulls and assigning to .net variable. But this inconvenience is no more now because of the Nullable types. A nullable type can be declared similar to normal variable but with ? modifier at the end of keyword.

i.e., a valid nullable type declaration will be,

 

int? mobile = null;

Alternatively we can declare like,

 

Nullable<int> mobile = null;

 

int? will be the shorthand notation of Nullable<int>.

 

Things to know about Nullable Types:

 

Nullabe types are instances of System.Nullable struct.Nullable type structure differs from normal type by having flag extra to indicate whether it contain null value.Additional it have 2 other properties which we will be looking in detail in this article.

 

Working with Nullable Types:

How to retrieve the value from a nullable type?

 

As we discussed early it has 2 other property called HasValue and Value.



 

Value:

This property is a read only property which retrieves the value associated with the type.This property will throw an exception when it is accessed with a null value.

 

int? mobile = null;

int mymobile = mobile.Value;

 

The above line will throw a exception,

 

“Invalid operation exception”

Nullable object should have value.

 

HasValue:

This property returns a Boolean indicating whether the variable contains a value.

 

So the above exception can be prevented by writing the code like,

if (mobile.HasValue)

{

int mymobile = mobile.Value;

}

 

Consider a situation like,

 

int? n1 = 2;

int n2 = 3;

 

Now what will be output type of (n1*n2) ?

 

int sum = n1 * n2;

 

The above operation will throw an error.

This operation gives a result which a nullable type can hold and thus it can be assigned to a variable like,

 

int? sum = n1 * n2;

 

We can call these operators as Lifted operator’s i.e., it will lift the non nullable type to nullable type. Assigning null to any of the above variable will result null. The same behavior applies to + and – operators.

 

Getting a default value with Nullable Types:

 

int? n1 = null;

int n2 = 3;

 

(n1 ?? 10) will return the value 10.This will be useful when we are fetching a allow null field from database and assigning to a non nullable type or it can be used in a operation like below. Hence we can assign a default value.

 

int product = (n1 ?? 10) * n2;

 

Now product will hold 30 since (n1 ?? 10) will return 10.

 

Note:

We cannot create nullable types with reference types.

 

 

 

Similar Articles
You can contribute to CodeDiget.Com:
Donate to CodeDigest.com
Article Feedback
Comments
Good one
Really, it has everything one is look for. simple though full of concepts.

Lots of thanks from my part.
Nullable Types in C#
Actually we good concept and also have learne a new concept.
a new concept from codedigest....
thanks