What is the purpose of Nullable types in .NET

No.of Views778
Bookmarked0 times
Downloads 
Votes0
By  pranay rana   On  18 Jan 2011 09:01:33
Tag : CSharp , CSharp4.0
Nullable type is new concept introduced in C#2.0 which allow user to assingn null value to primitive data types of C# language. Important to not here is Nullable type is Structure type.
emailbookmarkadd commentsprint

Images in this article missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at info@codegain.com

 

Introduction

In this article i will explain what is purpose of the Nullable type in .NET. The Nullable type is new concept introduced in C#2.0 which allow user to assign null value to primitive data types of C# language. Important to not here is Nullable type is Structure type.

What is nullable type ?

Nullable type is new concept introduced in C#2.0 which allow user to assingn null value to primitive data types of C# language. Important to not here is Nullable type is Structure type.

Why nullable types needed in programming?

In database missing information in records represented by null values even if its primitive types of data like varchar, int, datetime etc. So the problem arises when fetch data from the data base, how to represent missing null value in our program because primitive data only stores real values in C#.

For example

Consider system where I maintaining one city people records, which stores birth date and death date to calculate the age. But database stores null value in death date field for the people who is still alive.

Pseudocode code - formula for the age calculation is

//for the people who is already dead  
age = deathdate - birthdate;       
//for the people who is till alive
age = todays date - birthdate;

Solution 1

Before introduction of nullable vaue in older days we use some junk value to represent value is not present. For example

int a = -99;
DateTime dt = new DateTime(1901, 1, 1);

Pseudocode code for the above problem is

age = deathdate - birthdate;       
//for the people who is till alive -- check date value is equal to dt i.e 1/1/1901
age = todays date - birthdate;

So we compare this value and get information that value is not real value.

Problem with this is we have to remember this value and have to hardcode this value in our program structure.
Solution 2

Nullable type Syntax

Nullable<int> variable= null;
or
int? variable= null;

Properties

Nullable types has two important property

  • Hasvalue - indicate that variable contains real values or not. It returns false if variable contains no value and true if some real values is stored in it.
  • Value - Return real value stored in variable. It throws InvalidOperationException exception.

Methods

GetValueOrDefault - Return value stored by Nullable type variable or the value passed in function.
There are two overload of this function

Syntax

  • GetValueOrDefault() - returns real value or the default value of object.
  • GetValueOrDefault(T) - returns real value or the value passed.

Pseudocode Code for above problem

//variable define
DateTime? deathdate = null;
 
//for the people who is already dead  
if( deathdate.Hasvalue) // checking for the nullable varialbe has value or not
age = deathdate.Value - birthdate;       
//for the people who is till alive
else
age = todays date - birthdate; 

References

Summary

I hope now people why the Nullable feature get included in C#2.0.

 
Sign Up to vote for this article
 
About Author
 
pranay rana
Occupation-CEO
Company-GMind Solusion
Member Type-Senior
Location-India
Joined date-08 Jan 2011
Home Page-http://pranayamr.blogspot.com
Blog Page-http://pranayamr.blogspot.com
Hey, I am Pranay Rana, working as a Senior Software engineer in mid-size company located in ahmedabad. Web development in Asp.Net with C# and MS sql server are the experience tools that I have had for the past 4.3 years now. For me def. of programming is : Programming is something that you do once and that get used by multiple for many years You can visit me on my blog - http://pranayamr.blogspot.com/ StackOverFlow - http://stackoverflow.com/users/314488/pranay My CV :- http://careers.stackoverflow.com/pranayamr
 
 
Other popularSectionarticles
    In this article, i will explain how to use the How to use ExpandoObject Class in .NET 4.0.The ExpandoObject class is introduced on .NET Framework 4.0 and inherited using many interfaces.
    Published Date : 16/Apr/2011
    C# 4.0 supports Dynamic Programming by introducing new Dynamic Typed Objects
    Published Date : 26/May/2010
    Dynamic Datatype in C# 4.0 is quit bit familiar to Var Datatype. Deference between Var and Dynamic is VAR initialized on CompileTime and Dynamic initialized On Runtime
    Published Date : 17/May/2010
    In this article I will show how to use the IObserver and IObservable Interfaces in C#. These two interfaces is work with connection to Push based approach on Reactive Framework
    Published Date : 15/Dec/2010
    Through this article, i will introduce in FCL named Tuple which can store n - number of values in it. Yes, you specify the type of each of those variables as generic parameters, and the object will create those values for you
    Published Date : 15/Nov/2010
Comments
There is no comments for this articles.
Leave a Reply
Title:
Display Name:
Email:
(not display in page for the security purphase)
Website:
Message:
Please refresh your screen using Ctrl+F5
If you can't read this number refresh your screen
Please input the anti-spam code that you can read in the image.
^ Scroll to Top