Exploring Nullable types : Part 1

In this post, I am going to talk about Nullable types. Actually most of developers know, in c# we have
mainly two types.
– Reference type
– Value type.
But we have 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 initialize to some value while 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

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

C# also provide a short hand syntax for this as

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 refelector how it got implemented. Here you can see the Class declaration as


public struct Nullable<T> where T: struct .  It means Nullable can be used over struct type i e value type.

Here we will the some main properties of  Nullable type and leave default ones. Now lets go to the details of method  public Nullable(T value). This is parameterised constructor. Now lets 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 defat it is set false and once we assign some value in it is set as true. So whenever one access it without assigning it a value it returns null reference.

Now lets see the HasValue property.

It is just returning the hasValue.
Now lets 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 return the value.

Als lets have a view two two more next methods public T GetValueOrDefault() and public T GetValueOrDefault(T defaultValue). As the mathod 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 it checks the hasValue and based on this return the result. Similarly if you want to see public override bool Equals(object other) mathos 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 Oberloading. But what is this for?  Lets see the code below

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 which underlying type is same as underlying type of a Nullable type without any casting.

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

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

But you always write as

 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 weel. Theare are a lot more thing still remaining that I’ll cover in my next Post.


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s