We use ?? Operator to evaluate expressions against null values, for example:
string foo = null;
string bar = "woooo";
string foobar= foo ?? bar ;
// Evaluates foobar as woooo
We also used an if statement, which works the same if used with above expression
string foo = null;
string bar = "woooo";
if(foo==null)
string foobar= "woooo" ;
// Evaluates foobar as woooo same as above
And also ?: Ternary Operator…
string foo = null;
string bar = "woooo";
string foobar= foo==null ? "woooo" : null ;
// Evaluates foobar as woooo same as above
I know that null coalescing is precise in syntax, but which one is compiled faster among both and performs more faster and why?
You may be asking the wrong question. You don’t choose to use one over the other primarily due to efficiency (though it may be a secondary concern), but due to utility.
Really you should compare
??to?:and not toifas they have different purposes. Yes, they are all some form of “conditional” goodness, but the key is that both??and?:evaluate to a value, whereasifdoes not, thus they often have different uses.For example, the following code:
Would be clunkier to write with an
if:Yes, you could condense to one, but then you’d have a temp variable, etc:
Thus really, you shouldn’t compare the two, because they point of the
??is to evaluate to a value if the argument isnull, whereas the point ofifis to execute a different path (not directly resulting in a value.So, a better question may be, why not this instead:
Which is much the same (both evaluate to a value) and are not as much for flow control.
So, let’s look at the difference. Let’s write this code three ways:
For the IF, we get the following IL:
For the conditional (? 🙂 we get the following IL:
For the null-coallescing (??) we get this IL:
Notice how each successive one is simpler? The
ifis larger IL because it needs branching logic to handle the separate statements. The?:is smaller because it simply evaluates to a value (not branching to other statements) but still needs to load the operand to compare to (null).The
??is the simplest of all, because there is an IL instruction for comparing tonull(vs loadingnulland comparing to it.SO all of this said, you’re talking a very small difference in terms of IL, which may or may not affect performance. Regardless, chances are this will have very little major difference compared to more intensive work in the program (math, database, network, etc.).
Thus, I would suggest choose the one that is the most readable, and only optimize if you find through profiling that your current method is inadequate and a bottleneck.
To me, the real reason to use
?:or??is when you want the end result to be a value. That is, anytime you’d be tempted to write:Then I’d use the conditional (
?:) because that is what it is a great shorthand for.xgets one or the other value based on this condition…Then I’d go one further with
??and say the same is true, you want to assign a value to a variable based on thenull-ness of an identiifer.So
ifis great for flow-control, but if you are just returning one of two values or assigning one of two values based on a condition, I’d use?:or??as appropriate.Finally keep in mind how these things are implemented under the covers (IL and the associated performance) are subject to change with each revision of the .NET Framework (as of me writing this right now they are all so close to be negligible).
Thus, what may be faster today may not be faster tomorrow. So again, I’d just say go with the one that fits best and you find most readable.
UPDATE
Incidentally, for the truly obsessed, I compared 10,000,000 iterations of each code swatch above, and here’s the total time to execute each. Looks like
??is fastest for me, but again these are so close to be almost inconsequential…