Click here to Skip to main content
Click here to Skip to main content
Technical Blog

Tagged as

Null coalesce vs If null check

, 29 Sep 2011 LGPL3
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. Null coalesce operator in c# is ?? and you can use it as shown below:  String SomeValue;  var res = SomeValue ?? null Now, lets us dig [...]

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)

Share

About the Author

zenwalker1985
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/
Follow on   Twitter

Comments and Discussions

 
Bug[My vote of 2] IMHO, this test does compare different operations PinmemberDaniele Rota Nodari26-Oct-12 0:27 
GeneralMy vote of 1 PinmemberRay Parker29-Sep-11 23:01 
GeneralRe: My vote of 1 Pinmemberzenwalker19851-Oct-11 19:09 
Questioncan you check with adding assignment op Pinmembermrjer29-Sep-11 12:40 
I think, you are missing the assignment operation, result = null.
 
With same IL code, you will have same performance.
AnswerRe: can you check with adding assignment op Pinmemberzenwalker198529-Sep-11 17:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    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
Web04 | 2.8.141223.1 | Last Updated 29 Sep 2011
Article Copyright 2011 by zenwalker1985
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid