Click here to Skip to main content
12,695,739 members (30,149 online)
Click here to Skip to main content
Add your own
alternative version


24 bookmarked

The Null Coalescing Operator (??)

, 28 Sep 2007 CPOL
Rate this:
Please Sign up or sign in to vote.
One of the most useful yet little-known features to come out of C# 2.0


I'm constantly surprised by the number of developers who aren't aware of this handy piece of syntax. It's my favourite thing to come out of C# 2.0 and no developer should be without it.

Like the conditional (?:) operator's big brother... introducing it for your coding pleasure...

The Null Coalescing Operator (??)

The null-coalescing-operator is a brilliant new terse operator that provides syntax for beautifully concise if statements. Essentially, it returns the left-hand-side of the ?? operator, unless null, in which case it executes and returns the right-hand-side of the operator. This may be a statement or a variable reference. Let's jump straight to some examples:

// used inline outputs the value foo or if null returns Undefined
Console.WriteLine("The value of foo is " + (foo ?? "Undefined") + ".");
Input:  foo = "24";
Output: The value of foo is 24.
Input:  foo = null;
Output: The value of foo is Undefined.

The operator is right-associative meaning statements can be chained together; thus returning the first non-null instance:

// assigns foo to the first non-null instance, else returns Undefined
string foo = foo1 ?? foo2 ?? foo3 ?? foo4 ?? "Undefined";
Console.WriteLine("The value of foo is " + foo + ".");

Input:  foo1 = null;
        foo2 = null;
        foo3 = null;
        foo4 = null;
Output: The value of foo is Undefined.
Input:  foo1 = null;
        foo2 = "foo2";
        foo3 = null;
        foo4 = "foo4";
Output: The value of foo is foo2.

Handling null ViewState references:

// try to assign ViewState value as an int, else if null assign 123
int foo = (int?)ViewState["foo"] ?? 123;
Response.Write("The value of foo is " + foo + ".");
Input:  ViewState["foo"]=1;
Output:  The value of foo is 1.  
Input:  ViewState["foo"]=null;
Output:  The value of foo is 123.  

And my personal favorite, on demand field instantiation:

private IList<string> foo;

public IList<string> Foo
        return foo ?? (foo = new List<string>());

Here's an interesting example derived from an idea in the discussions below. It shows how an operator override can be used within an object's definition to enable shorthand syntax for double-null checking. The scenario is checking an object property for null using a null-coalescing-operator, but also defaulting when null-object-reference occurs; which would normally cause a runtime exception. (Note that I don't recommend actually using this approach, I just thought it made an interesting example.)

public class Address
    private static Address Empty = new Address();
    public string StreetName = null;
    public static Address operator +(Address address)
        return address ?? Empty
Console.WriteLine("The street name is "+ (+address).StreetName ?? "n/a" + ".");
Input:  address = new Address();
Output:  The street name is n/a.  
Input:  address = new Address();
            address.StreetName = "Regent St";
Output:  The street name is Regent St.    
Input:  address = null;
Output:  The street name is n/a.

The Rules

To use the null-coalescing-operator, there are some compile-time ground rules.

  • The left-hand-side must evaluate to a reference or nullable type.
  • All evaluated statements must be of matching type, unless they can be implicitly converted.


As you can see from the examples above, this little gem is very powerful and the possibilities are endless. Of course the benefits are purely syntactical, but it helps keep the code clean and easier to follow. I hope you enjoy it as much as I do.


  • 28th September, 2007: Initial post


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


About the Author

United Kingdom United Kingdom
Mike Carlisle - Technical Architect with over 20 years experience in a wide range of technologies.


You may also be interested in...

Comments and Discussions

GeneralAn alternate view Pin
Big Al 071-Oct-07 7:38
memberBig Al 071-Oct-07 7:38 
GeneralRe: An alternate view Pin
TheCodeKing1-Oct-07 9:18
memberTheCodeKing1-Oct-07 9:18 
GeneralRe: An alternate view Pin
Hal Angseesing1-Oct-07 10:06
memberHal Angseesing1-Oct-07 10:06 
GeneralRe: An alternate view Pin
Big Al 071-Oct-07 15:47
memberBig Al 071-Oct-07 15:47 
GeneralRe: An alternate view Pin
kschulz1-Oct-07 13:08
memberkschulz1-Oct-07 13:08 
GeneralRe: An alternate view Pin
Matware1-Oct-07 17:24
memberMatware1-Oct-07 17:24 
GeneralRe: An alternate view Pin
greenblob3-Oct-07 6:12
membergreenblob3-Oct-07 6:12 
GeneralRe: An alternate view Pin
reinux18-Oct-07 20:06
memberreinux18-Oct-07 20:06 
GeneralRe: An alternate view Pin
peterchen5-Jan-08 11:25
memberpeterchen5-Jan-08 11:25 
It all comes down to readability of the source code. Does the terse operator make the code more or less readable?

A terse construct can improve readability by expressing a common concept in a distinctive, non-redundant way, that can be conceived with least cognitive load. It can also worsen readability, by encoding an uncommon, yet complex construct in a simplistic, non-intuitive way.

For the operator in question, ther next best alternative to value ?? default would be either the ternary operator, value != null ? value : default;, or restructuring the code into an if/else block. Both introduce more code points to understand, and have an redundant occurance of value.

Worse, I could write code that looks like thi pattern, but isn't - either intentionally, or as a mistake:
object x = (Container.Matrix[i,j] != null) ? Container.Matrix[j,i] : Container.DefaultValue;

I don't think it's fair to compare assembler to this. Assembler has a completely different problem: the same idea can be expressed in many valid (and - depending on context preferrable) ways, e.g.
mov eax, [value]
or eax, eax
jz L1
mov eax, [default]

or by

xor ebx, ebx
mov esi, [v+ecx*0x04+0x20]
cmp esi, ebx
jne L1
mov [result], esi
jmp Exit
mov edi, [default]
mov [result], edi

The cognitive load to recognize the idea from these snippets is fairly high.

We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
My first real C# project | Linkify!| FoldWithUs! | sighist

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.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170118.1 | Last Updated 28 Sep 2007
Article Copyright 2007 by TheCodeKing
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid