These two code blocks are functionally the same
if (myObj == null)
{
myObj = new MyObj();
}
and
myObj = myObj ?? new MyObj();
However, the one using the null coalescing operator does an unnecessary assignment in the case where myObj is not null. But then I thought maybe the compiler optimizes these self assignments. Does anyone know if the compiler will notice what is going on and essentially convert the bottom snippet into the top one?
For comparison purposes, I tried compiling both
object myObj = null;
myObj = myObj ?? new object();
and
object myObj = null;
if(myObject == null)
{
myObj = new object();
}
inside of the Main
method. (I am using MonoDevelop 2.4 on Mono 2.6.7)
If the code were optimized as expected, we should see similar IL being generated. Here is the IL for the first version of Main
:
.method public static hidebysig
default void Main (string[] args) cil managed
{
.entrypoint
.maxstack 3
.locals init (
object V_0)
IL_0000: ldnull
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: dup
IL_0004: brtrue IL_000f
IL_0009: pop
IL_000a: newobj instance void object::'.ctor'()
IL_000f: stloc.0
IL_0010: ret
}
and for the second version:
.method public static hidebysig
default void Main (string[] args) cil managed
{
.entrypoint
.maxstack 1
.locals init (
object V_0)
IL_0000: ldnull
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: brtrue IL_000e
IL_0008: newobj instance void object::'.ctor'()
IL_000d: stloc.0
IL_000e: ret
}
So the first version (using the null-coalescing operator) has generated slightly more IL.
But there are two things to note about this:
Just tried it in LinqPad:
void Main()
{
MyObj myObj = null;
myObj = myObj ?? new MyObj();
}
This gives the following IL:
IL_0000: ldnull
IL_0001: stloc.0
IL_0002: ldloc.0
IL_0003: dup
IL_0004: brtrue.s IL_000C
IL_0006: pop
IL_0007: newobj UserQuery+MyObj..ctor
IL_000C: stloc.0
So it seems the assignment (stloc.0
at IL_000C) is done whether or not myObj
was null... but perhaps the JIT optimizes this later.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With