Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6065849
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T09:26:13+00:00 2026-05-23T09:26:13+00:00

These two code blocks are functionally the same if (myObj == null) { myObj

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T09:26:14+00:00Added an answer on May 23, 2026 at 9:26 am

    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:

    1. This IL is what I got using MoveDevelop – if you compile it in Visual Studio, it could very well have been a different story. Perhaps it is optimized in Microsoft’s with compiler. And even if it’s the same with them, another person’s compiler could easily optimize this. Just because something holds for some compilers, doesn’t mean you would expect it from everybody’s compiler.
    2. The CLR makes use of a JIT. Even if this wasn’t optimized at compile-time, it probably would have been optimized at run-time. In fact, this may very well be why the compile isn’t so concerned about such a micro-optimization.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have these two pieces of code, wich one is more readable? foreach decimal
Are there any significant performance differences these two blocks of code? var element =
What is the difference between these two pieces of code type IInterface1 = interface
In C#, what is the difference (if any) between these two lines of code?
I have seen code around with these two styles , I am not not
I've recently created these two (unrelated) methods to replace lots of boiler-plate code in
There are a number of run-time differences in compatible code between these two versions
Imagine these two chunks of code residing in htaccess for speeding up the website.
The code causing a leak of one block is as follows: in = new
I use the Boost Test framework for my C++ code but there are two

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.