Click here to Skip to main content
15,860,844 members
Articles / Programming Languages / C# 4.0

Nullable Types in C#.NET

Rate me:
Please Sign up or sign in to vote.
4.87/5 (70 votes)
10 Dec 2014CPOL6 min read 266K   103   71
This article explains the details and use of Nullable Type in C#.NET.

Abstract

This article will help you to understand the Nullable type implementation in C#. This article also explains about Coalescing Operator and how CLR has special support for Nullable value type.

Introduction

As we all know, a value type variable cannot be null. That's why they are called Value Type. Value type has a lot of advantages, however, there are some scenarios where we require value type to hold null also.

Consider the following scenario:

Scenario 1: You are retrieving nullable integer column data from database table, and the value in database is null, there is no way you can assign this value to an C# int.

Scenario 2: Suppose you are binding the properties from UI but the corresponding UI don't have data. (For example, model binding in ASP.NET MVC or WPF). Storing the default value in model for value type is not a viable option.

Scenario 3: In Java, java.Util.Date is a reference type, and therefore, the variable of this type can be set to null. However, in CLR, System.DateTime is a value type and a DateTime variable cannot be null. If an application written in Java wants to communicate a date/time to a Web service running on the CLR, there is a problem if the Java application sends null because the CLR has no way to represent this and operate on it.

Scenario 4: When passing value type parameter to a function, if the value of parameter is not known and if you don't want to pass it, you go with default value. But default value is not always a good option because default value can also be a passed parameter value, so, should not be treated explicitly.

Scenario 5: When deserializing the data from XML or JSON, it becomes difficult to deal with the situation if the value type property expects a value and it is not present in the source.

Likewise, there are many scenarios we faced in our day to day life.

To get rid of these situations, Microsoft added the concept of Nullable types to the CLR. To Understand this, have a look at the logical definition of System.Nullable<T> Type:

(Please note that below code snippet is the logical definition for illustration purposes only. Taken from "CLR Via C#, 3rd edition", written by Jeffrey Richter)

C#
[Serializable, StructLayout(LayoutKind.Sequential)]
public struct Nullable<T> where T : struct
{
    // These 2 fields represent the state
    private Boolean hasValue = false; // Assume null
    internal T value = default(T);    // Assume all bits zero
    public Nullable(T value)
    {
        this.value = value;
        this.hasValue = true;
    }
    public Boolean HasValue { get { return hasValue; } }
    public T Value
    {
        get
        {
            if (!hasValue)
            {
                throw new InvalidOperationException(
                "Nullable object must have a value.");
            }
            return value;
        }
    }
    public T GetValueOrDefault() { return value; }
    public T GetValueOrDefault(T defaultValue)
    {
        if (!HasValue) return defaultValue;
        return value;
    }
    public override Boolean Equals(Object other)
    {
        if (!HasValue) return (other == null);
        if (other == null) return false;
        return value.Equals(other);
    }
    public override int GetHashCode()
    {
        if (!HasValue) return 0;
        return value.GetHashCode();
    }
    public override string ToString()
    {
        if (!HasValue) return "";
        return value.ToString();
    }
    public static implicit operator Nullable<T>(T value)
    {
        return new Nullable<T>(value);
    }
    public static explicit operator T(Nullable<T> value)
    {
        return value.Value;
    }
}

From the above definition, you can easily make out that:

  • Nullable<T> type is also a value type.
  • Nullable Type is of struct type that holds a value type (struct) and a Boolean flag, named HasValue, to indicate whether the value is null or not.
  • Since Nullable<T> itself is a value type, it is fairly lightweight. The size of Nullable<T> type instance is the same as the size of containing value type plus the size of a boolean.
  • The nullable types parameter T is struct, i.e., you can use nullable type only with value types. This is quite ok because reference types can already be null. You can also use the Nullable<T> type for your user defined struct.
  • Nullable type is not an extension in all the value types. It is a struct which contains a generic value type and a boolean flag.

Syntax and Usage

To use Nullable type, just declare Nullable struct with a value type parameter, T, and declare it as you are doing for other value types. NullableTypes/Nullable1.png
For example:

C#
Nullable<int> i = 1;
Nullable<int> j = null;

Use Value property of Nullable type to get the value of the type it holds. As the definition says, it will return the value if it is not null, else, it will throw an exception. So, you may need to check for the value being null before using it.

C#
Console.WriteLine("i: HasValue={0}, Value={1}", i.HasValue, i.Value);
Console.WriteLine("j: HasValue={0}, Value={1}", j.HasValue, j.GetValueOrDefault());

//The above code will give you the following output:
i: HasValue=True, Value=5
j: HasValue=False, Value=0

Conversions and Operators for Nullable Types

C# also supports simple syntax to use Nullable types. It also supports implicit conversion and casts on Nullable instances. The following example shows this:

C#
// Implicit conversion from System.Int32 to Nullable<Int32>
int? i = 5;

// Implicit conversion from 'null' to Nullable<Int32>
int? j = null;

// Explicit conversion from Nullable<Int32> to non-nullable Int32
int k = (int)i;

// Casting between nullable primitive types
Double? x = 5; // Implicit conversion from int to Double? (x is 5.0 as a double)
Double? y = j; // Implicit conversion from int? to Double? (y is null)

You can use operators on Nullable types the same way you use it for the containing types.

  • Unary operators (++, --, -, etc) returns null if the Nullable types value is set to null.
  • Binary Operator (+, -, *, /, %, ^, etc) returns null if any of the operands is null.
  • For Equality Operator, if both operands are null, expression is evaluated to true. If either operand is null, it is evaluated to false. If both are not null, it compares as usual.
  • For Relational Operator (>, <, >=, <=), if either operand is null, the result is false and if none of the operands is null, compares the value.

See the example below:

C#
int? i = 5;
int? j = null;

// Unary operators (+ ++ - -- ! ~)
i++; // i = 6
j = -j; // j = null

// Binary operators (+ - * / % & | ^ << >>)
i = i + 3; // i = 9
j = j * 3; // j = null;

// Equality operators (== !=)
if (i == null) { /* no */ } else { /* yes */ }
if (j == null) { /* yes */ } else { /* no */ }
if (i != j) { /* yes */ } else { /* no */ }

// Comparison operators (< > <= >=)
if (i > j) { /* no */ } else { /* yes */ }

The Coalescing Operator

C# provides you quite a simplified syntax to check null and simultaneously assign another value in case the value of the variable is null. This can be used in Nullable types as well as reference types.

For example, the code below:

C#
int? i = null;
int j;

if (i.HasValue)
    j = i.Value;
else
    j = 0;
    
//The above code can also be written using Coalescing operator:
j = i ?? 0;

//Other Examples:
string pageTitle = suppliedTitle ?? "Default Title";
string fileName = GetFileName() ?? string.Empty;
string connectionString = GetConnectionString() ?? defaultConnectionString;
// If the age of employee is returning null
// (Date of Birth might not have been entered), set the value 0.
int age = employee.Age ?? 0;

//The Coalescing operator is also quite useful in aggregate function 
//while using linq. For example,
int?[] numbers = { };
int total = numbers.Sum() ?? 0;

// Many times, it is required to Assign default, if not found in a list.
Customer customer = db.Customers.Find(customerId) ?? new Customer();

//It is also quite useful while accessing objects like QueryString, 
//Session, Application variable or Cache.
string username = Session["Username"] ?? string.Empty;
Employee employee = GetFromCache(employeeId) ?? GetFromDatabase(employeeId);

You can also chain it, which may save a lot of coding for you. See the example below:

C#
// Here is an example where a developer is setting the address of a Customer. 
// The business requirement says that:
// (i) Empty address is not allowed to enter 
// (Address will be null if not entered). (ii) Order of precedence of 
// Address must be Permanent Address which if null, Local Address which if null, 
// Office Address.
// The following code does this:
string address = string.Empty;
string permanent = GetPermanentAddress();
if (permanent != null)
    address = permanent;
else
{
    string local = GetLocalAddress();
    if (local != null)
        address = local;
    else
    {
        string office = GetOfficeAddress();
        if (office != null)
            address = office;
    }
}

//With Coalescing Operator, the same can be done in a single expression.//
string address = GetPermanentAddress() ?? GetLocalAddress() 
                     ?? GetOfficeAddress() ?? string.Empty;

The code above with Coalescing operator is far easier to read and understand than that of a nested if else chain.

Boxing and UnBoxing of Nullable types

Since I have mentioned earlier that the Nullable<T> is still a value type, you must understand performance while boxing and unboxing of Nullable<T> type.

The CLR executes a special rule to box and unbox the Nullable types. When CLR is boxing a Nullable instance, it checks to see if the value is assigned null. In this case, CLR does not do anything and simply assigns null to the object. If the instance is not null, CLR takes the value and boxes it similar to the usual value type.

While unboxing to Nullable type, CLR checks If an object having its value assigned to null. If yes, it simply assigns the value of Nullable type to null. Else, it is unboxing as usual.

C#
// Boxing Nullable<T> is null or boxed T
int? n = null;
Object o = n;                                   // o is null
Console.WriteLine("o is null={0}", o == null);  // results to "True"

n = 5;
o = n; // o refers to a boxed Int32
Console.WriteLine("o's type={0}", o.GetType()); // results to "System.Int32"

// Create a boxed int
Object o = 5;
// Unbox it into a Nullable<int> and into an int
int? a = (Int32?) o;                            // a = 5
int b = (Int32) o;                              // b = 5

// Create a reference initialized to null
o = null;
// "Unbox" it into a Nullable<int> and into an int
a = (int?) o;                                   // a = null
b = (int) o;                                    // NullReferenceException

Calling GetType() for Nullable Type

When calling GetType() for Nullable<T> type, CLR actually lies and returns the Type the Nullable type it holds. Because of this, you may not be able to distinguish a boxed Nullable<int> was actually a int or Nullable<int>.
See the example below:

C#
int? i = 10;
Console.WriteLine(i.GetType()); // Displays "System.Int32" instead of  "System.Nullable<Int32>"

Points of Interest

Note that I haven't discussed the details of memory allocation and object creation while boxing and unboxing to keep the article focused to Nullable types only. You may Google it for details about boxing and unboxing.

Conclusion

Since Nullable Type is also a value type and fairly lightweight, don't hesitate to use it. It is quite useful in your data driven application.

References

History

  • 1st November, 2011: Version 1.0

License

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


Written By
Architect
India India
Anurag Gandhi is a Freelance Developer and Consultant, Architect, Blogger, Speaker, and Ex Microsoft Employee. He is passionate about programming.
He is extensively involved in Asp.Net Core, MVC/Web API, Node/Express, Microsoft Azure/Cloud, web application hosting/architecture, Angular, AngularJs, design, and development. His languages of choice are C#, Node/Express, JavaScript, Asp .NET MVC, Asp, C, C++. He is familiar with many other programming languages as well. He mostly works with MS SQL Server as the preferred database and has worked with Redis, MySQL, Oracle, MS Access, etc. also.
He is active in programming communities and loves to share the knowledge with others whenever he gets the time for it.
He is also a passionate chess player.
Linked in Profile: https://in.linkedin.com/in/anuraggandhi
He can be contacted at soft.gandhi@gmail.com

Comments and Discussions

 
QuestionThanks Pin
rtz877-May-15 21:12
rtz877-May-15 21:12 
AnswerRe: Thanks Pin
Anurag Gandhi7-Sep-16 23:56
professionalAnurag Gandhi7-Sep-16 23:56 
GeneralMy vote of 5 Pin
Santosh K. Tripathi1-Mar-15 21:39
professionalSantosh K. Tripathi1-Mar-15 21:39 
AnswerRe: My vote of 5 Pin
Anurag Gandhi15-Apr-21 6:13
professionalAnurag Gandhi15-Apr-21 6:13 
QuestionSuggestion Pin
KP Lee9-Dec-14 15:37
KP Lee9-Dec-14 15:37 
AnswerRe: Suggestion Pin
Anurag Gandhi9-Dec-14 17:19
professionalAnurag Gandhi9-Dec-14 17:19 
GeneralMy vote of 2 Pin
STeRennLT9-Dec-14 5:04
STeRennLT9-Dec-14 5:04 
GeneralRe: My vote of 2 Pin
wangfnso9-Dec-14 15:52
wangfnso9-Dec-14 15:52 
Question[My vote of 1] My Vote is 1 Pin
BeeGone16-Oct-14 4:54
BeeGone16-Oct-14 4:54 
AnswerRe: [My vote of 1] My Vote is 1 Pin
Anurag Gandhi16-Oct-14 5:39
professionalAnurag Gandhi16-Oct-14 5:39 
GeneralRe: [My vote of 1] My Vote is 1 Pin
Aurimas7-Dec-14 22:55
Aurimas7-Dec-14 22:55 
GeneralRe: [My vote of 1] My Vote is 1 Pin
Anurag Gandhi7-Dec-14 23:41
professionalAnurag Gandhi7-Dec-14 23:41 
His points are valid but the way he wrote was an insult instead of feedback.
I would not like to debate on it.

I have already updated my article based on his comment (whatever was possible).

Its not only about 10-15 people, when you need information, you really look for detailed explanation which should be more than mere documentation like msdn. If everyone understand from MSDN, then I agree, there should not be any tutorial article on any .net topic(s).
Life is a computer program and everyone is the programmer of his own life.

GeneralRe: [My vote of 1] My Vote is 1 Pin
Julijan Sribar9-Dec-14 5:30
Julijan Sribar9-Dec-14 5:30 
GeneralRe: [My vote of 1] My Vote is 1 Pin
Anurag Gandhi9-Dec-14 5:52
professionalAnurag Gandhi9-Dec-14 5:52 
GeneralRe: [My vote of 1] My Vote is 1 Pin
Julijan Sribar10-Dec-14 8:20
Julijan Sribar10-Dec-14 8:20 
GeneralRe: [My vote of 1] My Vote is 1 Pin
Anurag Gandhi10-Dec-14 23:30
professionalAnurag Gandhi10-Dec-14 23:30 
QuestionMy vote of 5 Pin
Toni Fasth25-Sep-14 9:04
professionalToni Fasth25-Sep-14 9:04 
AnswerRe: My vote of 5 Pin
Anurag Gandhi26-Sep-14 0:50
professionalAnurag Gandhi26-Sep-14 0:50 
GeneralMy vote of 5 Pin
Mladen Borojevic24-Sep-14 22:03
professionalMladen Borojevic24-Sep-14 22:03 
GeneralRe: My vote of 5 Pin
Anurag Gandhi26-Sep-14 0:49
professionalAnurag Gandhi26-Sep-14 0:49 
GeneralMy vote of 5 Pin
Humayun Kabir Mamun24-Sep-14 19:00
Humayun Kabir Mamun24-Sep-14 19:00 
GeneralRe: My vote of 5 Pin
Anurag Gandhi26-Sep-14 0:49
professionalAnurag Gandhi26-Sep-14 0:49 
GeneralMy vote of 5 Pin
Rajesh B Patil5-Jul-13 7:28
Rajesh B Patil5-Jul-13 7:28 
GeneralRe: My vote of 5 Pin
Anurag Gandhi24-Dec-13 7:51
professionalAnurag Gandhi24-Dec-13 7:51 
GeneralMy vote of 5 Pin
fredatcodeproject24-May-13 1:51
professionalfredatcodeproject24-May-13 1:51 

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.