views:

620

answers:

8

I recently found that we can use ?? operator to check nulls. Please check the below code samples:

   var res = data ?? new data();

This is exactly similar to

   var res = (data==null) ? new data() : data ;

I checked my whole project source repository and some of other open source projects. And this ?? operator never been used.

I just wondering is there any reason behind this, like performance problems or something?

EDIT:

I just updated my sample code based on the comments from recursive & Anton. Its a mistake in careless. :(

+32  A: 

The null coalesce operator is much clearer when checking for null, that is its main purpose. It can also be chained.

object a = null;
object b = null;
object c = new object();
object d = a ?? b ?? c; //d == c.

While that operator is limited to null checking, the ternary operator is not. For example

bool isQuestion = true;
string question = isQuestion ? "Yes" : "No";

I think people just aren't aware of the null coalesce operator so they use the ternary operator instead. Ternary existed before C# in most C style languages so if you don't know C# inside and out and/or you programmed in another language, ternary is a natural choice. If you are checking for null though, use the null coalesce operator, it is designed for that, and the IL is slightly optimized (compare ?? to an if then else).

Here is an example comparing the use of each

object a = null;
object b = null;
object c = null;

object nullCoalesce = a ?? b ?? c;

object ternary = a != null ? a : b != null ? b : c;

object ifThenElse;

if (a != null)
 ifThenElse = a;
else if (b != null)
 ifThenElse = b;
else if (c != null)
 ifThenElse = c;

First, just look at the syntax for null coalesce, it is way clearer. Ternary is really confusing. Now lets look at the IL

Null Coalesce Only

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object nullCoalesce)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: dup 
L_000c: brtrue.s L_0015
L_000e: pop 
L_000f: ldloc.1 
L_0010: dup 
L_0011: brtrue.s L_0015
L_0013: pop 
L_0014: ldloc.2 
L_0015: stloc.3 
L_0016: ldloc.3 
L_0017: call void [mscorlib]System.Console::WriteLine(object)
L_001c: ret

Ternary Only

.entrypoint
.maxstack 2
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ternary)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brtrue.s L_0016
L_000d: ldloc.1 
L_000e: brtrue.s L_0013
L_0010: ldloc.2 
L_0011: br.s L_0017
L_0013: ldloc.1 
L_0014: br.s L_0017
L_0016: ldloc.0 
L_0017: stloc.3 
L_0018: ldloc.3 
L_0019: call void [mscorlib]System.Console::WriteLine(object)
L_001e: ret

If Then Else Only

.entrypoint
.maxstack 1
.locals init (
    [0] object a,
    [1] object b,
    [2] object c,
    [3] object ifThenElse)
L_0000: ldnull 
L_0001: stloc.0 
L_0002: ldnull 
L_0003: stloc.1 
L_0004: newobj instance void [mscorlib]System.Object::.ctor()
L_0009: stloc.2 
L_000a: ldloc.0 
L_000b: brfalse.s L_0011
L_000d: ldloc.0 
L_000e: stloc.3 
L_000f: br.s L_001a
L_0011: ldloc.1 
L_0012: brfalse.s L_0018
L_0014: ldloc.1 
L_0015: stloc.3 
L_0016: br.s L_001a
L_0018: ldloc.2 
L_0019: stloc.3 
L_001a: ldloc.3 
L_001b: call void [mscorlib]System.Console::WriteLine(object)
L_0020: ret

IL isn't one of my strong points, so maybe someone can edit my answer and expand on it. I was going to explain my theory, but I'd rather not confuse myself and others. The number of LOC is similar for all three, but not all IL operators take the same length of time to execute.

Bob
It's not *the* ternary operator, it's just *one* ternary operator. What you refer to is called the **conditional operator**.
0xA3
@Bob, thats a nice comparison... coalesce operator is better than all when we deal with nulls...
Ramesh Vel
@divo: well it's the only ternary operator in C# :P
Lucas
Well, I'm a nitpicker. And conditional operator is the official term used in the C# reference ;)
0xA3
@Bob: was this IL from a Debug or Release build? You should really compare Release builds to see if there are any differences.
Matthew Whited
@Matthew Whited, Whoops, I forgot to change that, I am updating the IL right now, thanks!
Bob
I just did it myself and the nullCoalesce does make the most optimized version. I will post my code as another answer.
Matthew Whited
+5  A: 

One reason I can think of is that this operator was introduced in .NET 2.0 so the code for .NET 1.1 cannot have it.

I agree with you, we should be using this more often.

ref link

Binoj Antony
+11  A: 

The ?? operator (also known as the null-coalescing operator) is less known than the ternary operator, as it made its debut with .NET 2.0 and Nullable Types. Reasons for not using it probably include not begin aware that it exists, or being more familiar with the ternary operator.

That said, checking for null is not the only thing the ternary operator is good for, so it's not a replacement for it as such, more like a better alternative for a very specific need. :)

Rytmis
A: 

I think it's just a habit from other languages. AFAIK, ?? operator is not used in any other language.

Karel Bílek
A: 

I would have thought the equivalent of

var res = data ?? data.toString();

would be

var res = (data!=null) ? data : data.toString();
Bryan
+1  A: 

One reason (as others have already touched) is likely to be lack of awareness. It could also be (as in my own case), a wish to keep the number of approaches to do similar things in a code base down as much as possible. So I tend to use the ternary operator for all compact if-a-condition-is-met-do-this-otherwise-do-that situations.

For instance, I find the following two statements rather similar on a conceptual level:

return a == null ? string.Empty : a;    
return a > 0 ? a : 0;
Fredrik Mörk
A: 

Personally, it was lack of knowledge.

Thanks for the lesson!

JoelHess
+2  A: 

Based on Bob's answer

public object nullCoalesce(object a, object b, object c)
{
    return a ?? b ?? c;
}
public object ternary(object a, object b, object c)
{
    return a != null ? a : b != null ? b : c;
}
public object ifThenElse(object a, object b, object c)
{
    if (a != null)
        return a;
    else if (b != null)
        return b;
    else
        return c;
}

... this is the IL from release builds ...

.method public hidebysig instance object nullCoalesce(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: dup 
    L_0002: brtrue.s L_000b
    L_0004: pop 
    L_0005: ldarg.2 
    L_0006: dup 
    L_0007: brtrue.s L_000b
    L_0009: pop 
    L_000a: ldarg.3 
    L_000b: ret 
}

.method public hidebysig instance object ternary(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: brtrue.s L_000a
    L_0003: ldarg.2 
    L_0004: brtrue.s L_0008
    L_0006: ldarg.3 
    L_0007: ret 
    L_0008: ldarg.2 
    L_0009: ret 
    L_000a: ldarg.1 
    L_000b: ret 
}

.method public hidebysig instance object ifThenElse(
    object a, 
    object b, 
    object c) cil managed
{
    .maxstack 8
    L_0000: ldarg.1 
    L_0001: brfalse.s L_0005
    L_0003: ldarg.1 
    L_0004: ret 
    L_0005: ldarg.2 
    L_0006: brfalse.s L_000a
    L_0008: ldarg.2 
    L_0009: ret 
    L_000a: ldarg.3 
    L_000b: ret 
}
Matthew Whited