C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Resources » Articles » .NET Framework »

Nullability Types in C# 2.0


Posted Date: 02 Dec 2005    Resource Type: Articles    Category: .NET Framework
Author: radhika_vemuraMember Level: Bronze    
Rating: 1 out of 5Points: 5



Purpose
To provide the capability for value types to store null values.
In other words, nullability refers the ability of primitive data type to hold and handle null values.
Null generally refers to unknown or undefined value.
(Note - Even though we can assign null value to int variable, but still default value
i.e 0 gets stored unlike reference type.)

This plays an important role especially when interacting with databases containing fields which store null values and in other situations.

In the past, nullability handled in the following ways. This can be best explained with example below:

Example

[c#]

public int EmployeeCode()
{

int empCode = (int)Employee.Code // Code may be null when obtained from database

return empCode;

}

The above program throws an exception and can be handled in the following:

Method 1 : Implement Try-Catch block

public int EmployeeCode()
{

int empCode = 0;
try
{
empCode = (int)Employee.Code // Code may be null when obtained from database
}
catch(Exception ex)
{
empCode = -1;

}

return empCode;

}

Method 2 : Convert to Object to handle null value

public int EmployeeCode()
{

int empCode = 0;
object obj = Employee.Code ;

if(obj is null)
empCode = - 1;

else
empCode = (int)obj;


return empCode;

}

Method 3 : In above methods , consumer of method must be aware of - 1 means original value returned null. We can handle better manner using enums.

public enum returnType { null };

public int EmployeeCode()
{

int empCode = 0;
object obj = Employee.Code ;

if(obj is null)
empCode = (int)returnType.null;

else
empCode = (int)obj;


return empCode;

}

Other ways can be done using CustomClass, HashTable etc,

In other words c# 2.0, given us a feature called Nullability to achieve the same. This can achieved using ? type operator. int? denotes nullable form of int.

Design of Nullable type :
Nullable type is a structure that combines a value of the underlying type with a boolean null indicator. An instance of a nullable type has two public read-only properties: HasValue, of type bool, and Value, of the nullable type’s underlying type. HasValue is true for a non-null instance and false for a null instance. When HasValue is true, the Value property returns the contained value. When HasValue is false, an attempt to access the Value property throws an exception as below .

struct Nullable
{
public bool HasValue;
public T Value;
}

HasValue :
The HasValue property returns true if the variable contains a value, or false if it is null
The default value for a nullable type variable sets HasValue to false. The Value is undefined

Value :
The Value property returns a value if one is assigned, otherwise a System.InvalidOperationException is thrown


Note : refers generic type.

Syntax –

Nullable x = new Nullable(125);

More simpler way

int? X = 125;

Example – Let us take old example and see how it is achieved using Nullability
public string EmployeeCode()
{

int? empCode = Employee.Code ;

if(empCode.HasValue)
empCode = empCode.Value.ToString();

return empCode;
}
Other Features - (Coming in NextArticle)
a) Lifted operators in Nullable types
b) Comparisions in Nullable types
c) Conversions
d) Limitations

Radhika Vemura
MCAD,MCSD.NET



Responses

Author: Brainstorming Guy    05 Dec 2005Member Level: Diamond   Points : 0
Hi,
Looks good... Expecting a lot from you...
I have a small confusion.
Why there are 2?
int? X = 125;

and

struct Nullable
{
public bool HasValue;
public T Value;
}
and which one to use?

Regards,
Brainstorming Guy aka Venkatarajan A


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Sign In to add tags.
(No tags found.)

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Exceptional Handling at all Levels
Previous Resource: Cryptography in .NET
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
More Resources



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use