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 8831131
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:14:38+00:00 2026-06-14T08:14:38+00:00

We use ?? Operator to evaluate expressions against null values, for example: string foo

  • 0

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?

  • 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-06-14T08:14:39+00:00Added an answer on June 14, 2026 at 8:14 am

    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 to if as they have different purposes. Yes, they are all some form of “conditional” goodness, but the key is that both ?? and ?: evaluate to a value, whereas if does not, thus they often have different uses.

    For example, the following code:

    Console.WriteLine("The order} is for {1} product",
        orderId, productId ?? "every");
    

    Would be clunkier to write with an if:

    if (productId == null)
    {
        Console.WriteLine("The order {0} is for every product",
            orderId);
    }
    else
    {
        Console.WriteLine("The order {0} is for {1} product",
            orderId, productId);
    }
    

    Yes, you could condense to one, but then you’d have a temp variable, etc:

    if (productId == null)
    {
        productId = "every";
    }
    
    Console.WriteLine("The order {0} is for {1} product",
        orderId, productId);
    

    Thus really, you shouldn’t compare the two, because they point of the ?? is to evaluate to a value if the argument is null, whereas the point of if is to execute a different path (not directly resulting in a value.

    So, a better question may be, why not this instead:

    Console.WriteLine("The order {0} is for {1} product",
        orderId, productId == null ? "every" : productId);
    

    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:

    // Way 1 with if
    string foo = null;
    string folder = foo;
    
    if (folder == null)
    {
        folder = "bar";
    }
    
    // Way 2 with ? :
    string foo2 = null;
    var folder2 = foo2 != null ? foo2 : "bar";
    
    // Way 3 with ??
    string foo3 = null;
    var folder3 = foo3 ?? "bar";
    

    For the IF, we get the following IL:

    IL_0001:  ldnull      
    IL_0002:  stloc.0     
    IL_0003:  ldloc.0     
    IL_0004:  ldnull      
    IL_0005:  ceq         
    IL_0007:  ldc.i4.0    
    IL_0008:  ceq         
    IL_000A:  stloc.1     
    IL_000B:  ldloc.1     
    IL_000C:  brtrue.s    IL_0016
    IL_000E:  nop         
    IL_000F:  ldstr       "bar"
    IL_0014:  stloc.0     
    

    For the conditional (? 🙂 we get the following IL:

    IL_0001:  ldnull      
    IL_0002:  stloc.0     
    IL_0003:  ldloc.0     
    IL_0004:  brtrue.s    IL_000D
    IL_0006:  ldstr       "bar"
    IL_000B:  br.s        IL_000E
    IL_000D:  ldloc.0     
    IL_000E:  nop         
    IL_000F:  stloc.1   
    

    For the null-coallescing (??) we get this IL:

    IL_0001:  ldnull      
    IL_0002:  stloc.0     
    IL_0003:  ldloc.0     
    IL_0004:  dup         
    IL_0005:  brtrue.s    IL_000D
    IL_0007:  pop         
    IL_0008:  ldstr       "bar"
    IL_000D:  stloc.1    
    

    Notice how each successive one is simpler? The if is 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 to null (vs loading null and 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:

    if (someCondition)
        x = value1;
    else 
        x = value2;
    

    Then I’d use the conditional (?:) because that is what it is a great shorthand for. x gets 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 the null-ness of an identiifer.

    So if is 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…

    10,000,000 iterations of:
    ?: took: 489 ms, 4.89E-06 ms/item.
    ?? took: 458 ms, 4.58E-06 ms/item.
    if took: 641 ms, 6.41E-06 ms/item.
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I use operator() as a subscript operator this way: double CVector::operator() (int i) const
I'm trying to use operator overloading to define the basic operations (+,-,*,/) for my
When must we use this operator by events? What is its usage?
When must we use checked operator in C#? Is it only suitable for exception
I know that we cant use assignment operator in if statements in java as
While using the vector why do we sometime use the operator[] like homework[mid] but
I want to derive a stringstream so that I can use the operator<< to
How do I use the and operator '|' to compute 2 node sets? In
I'm wondering if you can overload an operator and use it without changing the
Can anybody please tell me what is the use of %NOTFOUND operator? We can

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.