Posted in C#, CodeProject, Dotnet

Null coalesce vs If null check

Dear Reader,

In this article, i shall talk about internals of null checking on a type (ref type mostly) by using null coalesce operator or via if condition.

Null coalesce operator in c# is ?? and you can use it as shown below:

  1.  String SomeValue;
  2.  var res = SomeValue ?? null

Now, lets us dig into the internals of IL , if IL is generated for the below code:

  1.  String SomeValue;
  2.  var res = SomeValue ?? null
  3.  if (SomeValue == null)
  4.     ;

The IL is as shown:

As you can see from the above IL code, line 17-23 is the code for Null coalesce operator and from line 25 – 30 is for if condition. As you can see IL instructions generated by compiler is almost same in number viz. ~6 opcodes for both conditional statements. So logically it looks like both doesn’t have any run time performance benefits for us.

So one might wonder, which one to use? I would say you can’t have else part condition for a null coalesce operate. So in this case, you cant use null coalesce condition checking.

But before concluding, lets also do some run time performance analysis on them. For that, i have written this following test code:

(click on image for better view)

As per the above test code with 1000 iterations, this is the result i got:

As you can see from the multiple output samples, it shows that the first sample output is taking more time. I am guessing this is because many objects have to be created for the first time since the application loads up. So its better to discard that sample. But as per the rest 4 samples is concerned it shows that the performance output (all values are in Ticks) for null coalesce is a bit more than if condition one.

Now lets reduce the iteration to 100 each and see, how it behaves. Below is the output:

Again as you can see, the difference is more now. Although i ran for multiple times and got the similar performance output in both the cases.

So now we can say that its better to use if condition to check for null for an object even though we get a small gain in performance.

Thanks & Happy coding:)


  1. Upon changing the ref type i.e string in above code to a nullable int, i seem to get the similar output. Hence performance is not influenced by the types here.
  2. Also based on suggestion i re-tested by adding “result = null” in the if condition body under second for loop in the above code. So after doing so here is the output samples i got:




Simple, avid, curious programmer. Nature hiker, animal lover, FOSS maniac by default.

4 thoughts on “Null coalesce vs If null check

  1. As I told you, seeing this makes me think of a little mistake.

    Actually if look at the IL of the Null Coalesce Operator it says
    brtrue.s IL000b

    Where IL000b says stloc.0

    that means when duplicate (dup) value of the same object is compared with stloc.0 (original object) and comparison evaluates to true, assign stloc.0.

    Otherwise it will get ldnull.

    This dup is expensive and creates replica of your original object for comparison and stores in evaluation stack.

    That means it will say the Runtime to check with null if it is true with first operand and then otherwise assign something ( in ur case it is null).

    Same is when you do like

    if(obj == null)
    result = obj;

    But if stores the result of the equality into a variable. In your IL you can see ldc.i4.0 which indicates that it pushes integer equivalent of the equality in stack stloc.1(first location). This is because if can have multiple else .. and if you dont store it to a storage, it would be difficult to handle the IL with jumps.

    Hence both of them are pretty much equal.

    BTW, you missed out the assignment operation in your example that makes if to be more faster than the other.

  2. Nope abhi, i did try both cases as well. And seem to get the similar output results. Hence i didn’t post it.

    But besides this, i don’t understand how they are almost equal when the performance figures are varying?

  3. Well, I have tried your sample with additional assignment in If and saw this output

    Actually they are almost same but not exactly. As you can see the performance figures is slightly different, with Null Coalesce operation is slightly higher than If. This is because of dup statement in null coalesce operator.

    What do you think ?

  4. I have edited my post again to answer you. And its strange you getting a closer difference than mine. But hey is it because i am doing all this on a Win 7 under Virtual box running on Linux? I have configured virtual box to provide only 1 core of my processor to win 7 or virtual box.

    Oh well, i have dedicated win 7 here. I rarely use it though. Any ways i shall boot in there and check it again sir. Thanks:)

    UPDATE: I checked on dedicated win 7 here. With less number of iterations viz 100, 200 i seem to get very close difference which we can easily rule out. But upon increasing to 1000 or double of it, there comes a bit of more difference.

Leave a Reply

Please log in using one of these methods to post your comment: Logo

You are commenting using your 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 )

Google+ photo

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

Connecting to %s