12,950,184 members (40,132 online)
Technical Blog
alternative version

#### Stats

12.8K views
Posted 17 Oct 2011

# Exploring Nullable types: Part 2

, 14 Nov 2011 CPOL
 Rate this:
This is the second and last part of the post on Nullable type series.

This is second and last part of the post on Nullable type series. You can view the first part from here:

In this series, I will be talking about certain rules that we need to take care of while using Nullable type. I will be taking it scenario wise.

## First Scenario

Int? a=8;
object c = a;
long d = (long)c;

It will be compiled successfully but will throw an Invalid Cast Exception. Means, you cannot cast it to any other type except the underlying type which is here int although int type can be hold by long.

But the below lines will run successfully:

long d = (int)c;
long? d = (int)c;

Because here we are converting to an underlying type int which we can assign to long as it is a widening conversion.

Now let’s move to another point.

## Second Scenario

Guess what will be the output of the below lines:

int? b=5;
Console.WriteLine(b.GetType());

Is it System.Nullable<Int32>. No
This actually shows System.Int32. So beware.

## Few More Things

• We can use Unary operator over Nullable type. It will be null if it has no value or null else applies the operator.
• Binary operators also can be used with Nullable type. Give it a view:
int? b=5;
int? a = 7;
Console.WriteLine(a+b);

This will print 12. But what if:

int? b=5;
int? a=null;
Console.WriteLine(a+b);

This will print nothing because the addition result will be null. So be sure, during any binary operation if one operand (or does not have any value) is null. The result will also be null (or would not have any value).

Hope both posts will give you all a fair enough knowledge of Nullable types. Do share if you have some more points in this. I will add those here..

Happy C# coding.

## Share

 Software Developer (Senior) 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

## You may also be interested in...

 First Prev Next
 My vote of 1 Collin Jasnoch17-Oct-11 5:39 Collin Jasnoch 17-Oct-11 5:39
 Re: My vote of 1 Brij14-Nov-11 8:40 Brij 14-Nov-11 8:40
 Thanks for pointing it out. I'll update the code. It should be like long d = (long)o; Then only my statements hold true. Collin Jasnoch wrote:What are you trying to provide that MSDN is missing? My friend, I use MSDN a lot to read about any topic. But frankly speaking, it gives lots of content on a single topic and sometimes it become very tough to decide where to start. Even after reading some Article, We find that we didn't get the thing that we actually require. The Idea of these posts are, to highlight some basic things( that are really useful) and represent it in very simple words and also make it as small as possible. So the developers can use it in their daily tasks and get benefited. I hope this will make your confusion clear. Cheers!! Brij Microsoft MVP ASP.NET/IIS Visit my Blog: http://brijbhushan.net
 Re: My vote of 1 Collin Jasnoch14-Nov-11 9:43 Collin Jasnoch 14-Nov-11 9:43
 Last Visit: 31-Dec-99 18:00     Last Update: 25-May-17 14:36 Refresh 1