Click here to Skip to main content
13,353,706 members (59,333 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 29 Sep 2011

Null coalesce vs If null check

, 29 Sep 2011
Rate this:
Please Sign up or sign in to vote.
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.

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:

String SomeValue;
var res = SomeValue ?? null

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

 String SomeValue;
var res = SomeValue ?? null
if (SomeValue == null)

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 :)

EDIT: 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.


This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


About the Author

Software Developer (Senior) Siemens
India India
A .net developer since 4+ years, wild, curious and adventurous nerd.

Loves Trekking/Hiking, animals and nature.

A FOSS/Linux maniac by default Wink | ;)

An MVP aspirant and loves blogging ->

You may also be interested in...

Comments and Discussions

Bug[My vote of 2] IMHO, this test does compare different operations Pin
Daniele Rota Nodari26-Oct-12 0:27
memberDaniele Rota Nodari26-Oct-12 0:27 
GeneralMy vote of 1 Pin
Ray Parker29-Sep-11 23:01
memberRay Parker29-Sep-11 23:01 
GeneralRe: My vote of 1 Pin
zenwalker19851-Oct-11 19:09
memberzenwalker19851-Oct-11 19:09 
Even doing so i.e stopping it and then taking ticks plus adding a body to the if condition doesnt change the result much. Still i do get similar results. You can check my blog for updates.
Thanks for your vote as well Smile | :)
Questioncan you check with adding assignment op Pin
mrjer29-Sep-11 12:40
membermrjer29-Sep-11 12:40 
AnswerRe: can you check with adding assignment op Pin
zenwalker198529-Sep-11 17:05
memberzenwalker198529-Sep-11 17:05 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.180111.1 | Last Updated 29 Sep 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid