Click here to Skip to main content
15,881,715 members
Articles / All Topics

Null coalesce vs If null check

Rate me:
Please Sign up or sign in to vote.
1.50/5 (2 votes)
29 Sep 2011LGPL32 min read 11.7K   5
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.

License

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


Written By
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 -> https://adventurouszen.wordpress.com/

Comments and Discussions

 
Bug[My vote of 2] IMHO, this test does compare different operations Pin
Daniele Rota Nodari25-Oct-12 23:27
Daniele Rota Nodari25-Oct-12 23:27 
GeneralMy vote of 1 Pin
Ray Parker29-Sep-11 22:01
Ray Parker29-Sep-11 22:01 
GeneralRe: My vote of 1 Pin
zenwalker19851-Oct-11 18:09
zenwalker19851-Oct-11 18:09 
Questioncan you check with adding assignment op Pin
mrjer29-Sep-11 11:40
mrjer29-Sep-11 11:40 
AnswerRe: can you check with adding assignment op Pin
zenwalker198529-Sep-11 16:05
zenwalker198529-Sep-11 16: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.