Blue Theme Orange Theme Green Theme Red Theme
 
MindFusion's Components
Home | Forums | Videos | Photos | Downloads | Blogs | E-Books | Interviews | Jobs | Beginners | Training
 | Consulting  
Submit an Article Submit a Blog 
 Login Close
User Id:
Password:
 
Forgot Password
Forgot Username
Why Register
 Jump to
Skip Navigation Links
TechnologyExpand Technology
WebsiteExpand Website
 Resources  
Close
 Our Network  
Close
Search :       Advanced Search »
Home » C# Language » Nullable Types in C#: Basic Introduction

Nullable Types in C#: Basic Introduction

This article gives a quick view in to the problems faced with the value type objects and the solution to the problems. This beautiful facility is introduced by Microsoft as an concept called “Nullable Types” in the C# 2.0.

Technologies: .NET 1.0/1.1,Visual C# .NET
Total downloads :
Total page views :  24350
Rating :
 4.5/5
This article has been rated :  4 times
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
Powerful ASP.NET Hosting w/ NO Setup Fees. Click Here!
Become a Sponsor


Related EbooksTop Videos

Summary:

The Value type objects are faster for the accessing and operations as compared to their reference counterpart. This makes them as a preferred choice for the data storage in the program. However there are certain limitations while using the value type objects in the application. This article gives a quick view in to the problems faced with the value type objects and the solution to the problems. This beautiful facility is introduced by Microsoft as an concept called "Nullable Types" in the C# 2.0.

In this article I will focus to answer two questions mainly those are:

  1. Why we need Nullable Types?
  2. What are the Nullable Types?

a. Why need Nullable type.

1. What will be the default value for the value type object?

Assigning a default value to a value type object is really a brainstorming process. Since this involves the use of the probability matrix, through which one can evaluate the possible values that a value type object can be assigned during it's whole life span. Apart from finding the values that a object can have during it's life span we also need to lay down a standard to consider some arbitrary value that lie outside the probability matrix; as the default value. This forces one rule on the project standards to consider the designated value as a default value and then force to follow the rule on the people working on the project.

Apart from this working and forced standards limitation; this default value occupies the space in the value matrix as default and unusable value limiting the future value for the object.

Let's focus this with an example.

I have a structure about the student information I can write the structure as:

public struct student{

        private int m_RollNumber; // Can range from 0 -- 100

        private string m_FullName ; // the non blank string name.

        ..

        ..

        public student( int RollNumber, string FullName,.....){

        //Initialize the members of the new object

        this.m_RollNumber = RollNumber ;

        this.m_FullName = FullName;

        ..

        ..

}

Even though I can provide the value for the roll number in the constructor, The C# still allows me to call the default constructor with no arguments. The default constructor will initialize the m_RollNumber to the zero and the m_FullName to the blank string.  Since m_RollNumber =0 is a valid value in the value matrix for the m_RollNumber. To denote the un-initialized m_RollNumber we need to assign a value to the m_Rollnumber that can be considered as default. Here since I assume that the value for the m_RollNumber must be between 0 - 100 through it's life span. Based on this analysis I can consider values that are either Negative ( <0) or Positive (>100) as an default value. 

Lets Say I decide the value 999 as my default for the roll number; I need to force this rule to check the value being 999 before any body use the m_RollNumber in the code, and also the rule to think the value as 999 as default and denoting the un-initialized m_RollNumber.

Thinking of the future impact when the roll number reaches the value 1000, the value 999 will be useless even though being a valid roll number.

2.  The Functions returning the Value type object can not denote about the success of the execution:

Consider I have a collection of students where I search for any particular roll number.

public class Students

{

        //The Internal array list maintaining the student list

        private ArrayList m_Students = new ArrayList(100);

        public Student GetStudentInfo(int RollNumber){

            //Iterate through the list to find the student with roll number RollNumber

            foreach( student stTemp in m_Students){

                //Check if the stTemp is the required student.

                If( stTemp.m_RollNumber == RollNumber){

                    //if yes return the stTemp.

                    Return stTemp ;

                }

            }

            //If no match found return the empty object.

            Return new Student();

        }

}

The implementation of this function will return an object of the Student structure in all the cases even though the function fails to find the particular student[Since returning a blank object is done to suppress the compiler error.]. The caller of the function is still in ambiguity of the validity of the value returned. This is due to the fact that we can never assign the "null" value to the value type object.

To work around this problem we need to go for the 'Out' parameters. The modified version of the function is as follows:

public bool GetStudentInfo( int RollNumber, out Student outStudent){

      //Initialize the outStudent object to a blank object.

      outStudent = new Student();

      //Iterate through the loop to find the student with roll number as RollNumber

      foreach( student stTemp in m_Students)

{

            If( stTemp.m_RollNumber == RollNumber)

{

                   //If the required object found then

                   //Save it to the out parameter and

                   outStudent = stTemp ;

//return true to indicate the success.

Return true;

}

}

//If object is not found then simply return the false to denote

the failure

Return false;

}

By calling this version of the function the caller has a way to find the validity of the returned object and in turn to check the success of the function execution through the returned Boolean value. However changing the function signature this way makes the function hard to understand and call.

To overcome the above mentioned problems with the default value and the return value from the function. We need some tactful way around this problem.

That's the reason Microsoft has introduced the Nullable Types in the C# 2.0.

b. What are the Nullable types?

Nullable Types are the same generic data type with a wrapper structures written around the generic data type. This wrapper structures allow assigning the value "null" to the value type objects even.

If any object of the generic type has the '?' attached to the data type while declaring the value object. It denotes the object is a Nullable object of that data type e.g. if I declare the RollNumber as

int? RollNumber = null ;

The RollNumber declared above is Nullable roll number that means assigning the value null to the Rollnumber is valid and completely acceptable. This reduces the burden to make some value as default and also since the function can return the Nullable types as a return type we have a way to return a null value through the object when the function fails.

An instance of the Nullable type has two public properties.

  • HasValue - A Boolean property denoting the validity of the value contained in the object.
  • Value - A value contained in the object.

The 'true' value for the HasValue property guarantees the validity of the value contained in the object which can be accessed through the Value property. A 'false' value for the HasValue indicates the invalid value contained by the object suggesting not to use the Value property.

An instance for which HasValue is false is said to be null. A null instance has an undefined value. Attempting to read the Value of a null instance causes a System.InvalidOperationException to be thrown.

Taking about the default values for the Nullable type we can say; Any instance of a Nullable type is said to have a default value when the HasValue is set to 'false' and the value property is undefined. This value is known as an null value of an Nullable type. While implicitly converting the 'null' value to any nullable type values the conversion results in the null value of the nullable type.

The following example shows the use of the Nullable type.

class Program

{

    //the Nullable Roll number.

    public int? RollNumber = null;

    static void Main(string[] args)

    {

        Program pTemp = new Program();

        //use the public member.

        pTemp.RollNumber = 100;

        //Write to console to check the object values.

        Console.WriteLine("Value in RollNumber is:{0} ,[HasValue]{1}", pTemp.RollNumber.Value, pTemp.RollNumber.HasValue);

        //test using the null.

        pTemp.RollNumber = null;

        Console.WriteLine("Value in RollNumber is:{0} ,[HasValue]{1}", pTemp.RollNumber.Value, pTemp.RollNumber.HasValue); //This line throws and InValidOperationException.

    }

}

Some rules govern the type conversion between the Nullable and non-Nullable types this will be covered in the next articles [With a modified version of the GetStudentInfo function].


Login to add your contents and source code to this article
 [Top] Rate this article
 About the author
 
Prashant Patil
Hi, this is Prashant here. I am working as a Systems Executive with the KPIT Cummins Info Systems Ltd. Pune. Talking about my experience I have total of 3.5 Yrs of experience on the various languages scripting as well as non-scripting. I had also worked on the VC++(( MFC and COM) for almost 2.5 yrs along with 8 months on Java(Core) . Currently I am working on the .Net/C# since 1.5 yrs. I had also worked on the scripting languages as such JavaScript, VB Script etc along with HTML. Apart form programming, I use to play a role of designer in my project and has through understanding of the design patterns and UML to it's best.
Looking for C# Consulting?
C# Consulting is founded in 2002 by the founders of C# Corner. Unlike a traditional consulting company, our consultants are well-known experts in .NET and many of them are MVPs, authors, and trainers. We specialize in Microsoft .NET development and utilize Agile Development and Extreme Programming practices to provide fast pace quick turnaround results. Our software development model is a mix of Agile Development, traditional SDLC, and Waterfall models.
Click here to learn more about C# Consulting.
 
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications.
Go.NET
Build custom interactive diagrams, network, workflow editors, flowcharts, or software design tools. Includes many predefined kinds of nodes, links, and basic shapes. Supports layers, scrolling, zooming, selection, drag-and-drop, clipboard, in-place editing, tooltips, grids, printing, overview window, palette. 100% implemented in C# as a managed .NET Control. Document/View/Tool architecture with many properties&events. Optional automatic layout.
Dundas Software
Dundas Chart for .NET is the most advanced .NET charting package available today.  With an extremely complete feature set, elegant architecture and easy implementation, Dundas Chart can quickly add advanced Charting functionality to enhance and transform ASP.NET and Windows Forms applications.  Whether you are implementing charting into internal projects, or building applications for clients, Dundas Chart offers advanced technology and advanced results to get the most out of data.
Clickatell's SMS Gateway
Clickatell's Developer Solutions allow you to SMS enable any website or application via a range of API's. Learn More about our API connections.
Free access to .NET Memory Management video
Everything you need to know about Garbage Collection, Temporary Objects, Fragmentation, Finalization and common causes of memory leaks in .NET. Watch the video here.
Microsoft Visual Studio 2010
Microsoft Visual Studio 2010 offers more to developers than any other Visual Studio release. Work more productively and collaboratively-with greater control over your work at every step. The Beta 2 can give you a head start on achieving efficiency.
 
   Print Read/Post comments Post a comment  Rate  
   Email to a friend  Bookmark  Similar Articles  Author's other articles  
 
 Post a Feedback, Comment, or Question about this article
Subject:  
Comment:  
Become a Sponsor
 Comments

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Suggest an Idea  |  Media Kit
Current Version: 5.2009.6.2
 © 1999 - 2009  Mindcracker LLC. All Rights Reserved