Nullability Types in C# 2.0


This article explains purpose of introducing Nullability Types in C# 2.0

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


Comments

Author: Brainstorming Guy05 Dec 2005 Member Level: Gold   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



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: