Click here to Skip to main content
15,868,016 members
Articles / Programming Languages / C#

Exploring Nullable types: Part 1

Rate me:
Please Sign up or sign in to vote.
4.70/5 (3 votes)
17 Oct 2011CPOL3 min read 10.5K   1   5
In this post, I am going to talk about Nullable types.

In this post, I am going to talk about Nullable types. Actually most of developers know, in C#, we have mainly two types:

  1. Reference type
  2. Value type

But we have one more type that is called Nullable type.

Actually, it is a value type but it has feature of both reference and Value type.

As we know, a value type is allocated space on stack, and every value of type must be initialized to some value during declaration. If you don’t provide by yourself, C# does it for you. So every value type is assigned some value whether you initialize it or not.

Nullable type has a capability to hold a value or can have null reference, means there is no value. Nullable types were introduced in .NET 2.0.

We can declare and initialize a Nullable type as:

C#
//This is a Nullable of Int type
Nullable<int> t = 5;

C# also provides a short hand syntax for this as:

C#
int? i = 5;

If you would not assign a value, you can do it with Nullable types as we can do it with reference types.

Now let’s see it in the reflector how it got implemented. Here you can see the Class declaration as:

Nullable

C#
public struct Nullable<T> where T: struct

It means Nullable can be used over struct type, i.e., value type.

Here we will see some of the main properties of Nullable type and leave default ones. Now let's go to the details of method public Nullable(T value). This is parameterised constructor. Now let's see what is inside this code:

Here you can see that this is assigning value to the variable and setting hasValue as true.
So it actually has one property hasValue that does the entire job. By default, it is set to false and once we assign some value in it, it is set as true. So whenever one accesses it without assigning it a value, it returns null reference.

Now let's see the HasValue property.

It is just returning the hasValue.
Now let's see public T Value { get; } what it does:

As you can see whether hasValue is true or not. If false, throw some exception else it returns the value.

Also let's have a view at two more next methods public T GetValueOrDefault() and public T GetValueOrDefault(T defaultValue). As the method name suggests, that returns the existing value and if it is not set then return the type’s defult value. The definition is like this:

So you can see that it checks the hasValue and based on this, returns the result. Similarly if you want to see public override bool Equals(object other) method, it has the code like:

This also checks the hasValue before comparing it.

There are two operator’s method you can see at last. You all must be knowing this is Operator Overloading. But what is this for? Let's see the code below:

C#
int i = 5;
//Assigning a int type to a Nullable of int
Nullable<int> t = i;

Actually, here I have declared and intialised an int variable and created a nullable type and assigned the int variable to it. This is actually accomplished with the help of the operators.

So it means we can assign a value type to a Nullable type whose underlying type is the same as the underlying type of a Nullable type without any casting.

But vice versa is not possible. You have to do explicit cast as:

C#
Nullable<int> t = 5;
//Need an explicit cast
int i = (int)t;

But you always write as:

C#
Nullable<int> t = 5;
// No need for a cast because t.Value is actually the 
// underlying type and holds the value
int i = t.Value;

So I think you all must have enjoyed. There are a lot more things still remaining which I’ll cover in my next post.
Cheers,
Brij


License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India
Brij is a 3-times Microsoft MVP in ASP.NET/IIS Category and a passionate .NET developer. More than 6 years of experience in IT field, currently serving a MNC as a Tech Lead/Architect.

He is a very passionate .NET developer and have expertise over Web technologies like ASP.NET 2.0/3.5/4.0, jQuery, JSON, Javascript, IIS and related technologies. He is also a Exchange Server (EWS) Specialist. He has great experience in design patterns and N-Tier Architecture.

He is also certified as Microsoft Certified Technologies Specialist-ASP.NET and Microsoft Certified Technologies Specialist-WCF in .NET 4.0. He has also received several awards at various forums and his various articles got listed as "Article of the day" at ASP.NET Microsoft Official Website www.asp.net.

He has done MCA from NIT Durgapur and completed his graduation from Lucknow University.

Learning new technologies and sharing knowledge excites him most. Blogging, solving problems at various forums, helping people, keeps him busy entire day.


Visit his Blog: Code Wala

Area of Expertise :
C#, ASP.NET 2.0,3.5,4.0, AJAX, JQuery, JSON, XML, XSLT, ADO.Net, WCF, Active Directory, Exchange Server 2007 (EWS), Java script, Web Services ,Win services, DotnetNuke, WSS 3.0,Sharepoint Designer, SQL Server 2000/2005/2008

Comments and Discussions

 
GeneralMessage Removed Pin
17-Oct-11 5:33
professionalN_tro_P17-Oct-11 5:33 
GeneralRe: My vote of 2 Pin
Brij14-Nov-11 9:09
mentorBrij14-Nov-11 9:09 
GeneralMessage Removed Pin
14-Nov-11 9:31
professionalN_tro_P14-Nov-11 9:31 
GeneralRe: My vote of 2 Pin
Brij14-Nov-11 19:18
mentorBrij14-Nov-11 19:18 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.