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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T22:25:48+00:00 2026-05-22T22:25:48+00:00

null coalescing operator is right associative, which means an expression of the form first

  • 0

null coalescing operator is right associative, which means an expression of the form

first ?? second ??third

is evaluated as

first ?? (second ?? third)

Based on the above rule, I think the following translation is not correct.

From:

Address contact = user.ContactAddress;
if (contact == null)
{
    contact = order.ShippingAddress;
    if (contact == null)
    {
        contact = user.BillingAddress;
    }
}

To:

Address contact = user.ContactAddress ??
                  order.ShippingAddress ??
                  user.BillingAddress;

Instead, I think the following is right one (Please correct me if I am wrong)

Address contact = (user.ContactAddress ?? order.ShippingAddress) ??
                   user.BillingAddress;
  • 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-22T22:25:49+00:00Added an answer on May 22, 2026 at 10:25 pm

    The spec is actually self-contradictory on this one.

    Section 7.13 of the C# 4 spec states:

    The null coalescing operator is right-associative, meaning that operations are grouped from right to left. For example, an expression of the form a ?? b ?? c is evaluated as a ?? (b ?? c).

    On the other hand, as has been pointed out, 7.3.1 claims that:

    Except for the assignment operators, all binary operators are left-associative

    I entirely agree that for simple cases it doesn’t matter how you do the grouping… but there may be cases where it really matters due to implicit type conversions doing interesting things if the operands have different types.

    I’ll consider it further, ping Mads and Eric, and add an erratum for the relevant section of C# in Depth (which inspired this question).

    EDIT: Okay, I’ve now got an example where it does matter… and the null coalescing operator is definitely right-associative, at least in the MS C# 4 compiler. Code:

    using System;
    
    public struct Foo
    {
        public static implicit operator Bar(Foo input)
        {
            Console.WriteLine("Foo to Bar");
            return new Bar();
        }
    
        public static implicit operator Baz(Foo input)
        {
            Console.WriteLine("Foo to Baz");
            return new Baz();
        }
    }
    
    public struct Bar
    {
        public static implicit operator Baz(Bar input)
        {
            Console.WriteLine("Bar to Baz");
            return new Baz();
        }
    }
    
    public struct Baz
    {
    }
    
    
    class Test
    {
        static void Main()
        {
            Foo? x = new Foo();
            Bar? y = new Bar();
            Baz? z = new Baz();
    
            Console.WriteLine("Unbracketed:");
            Baz? a = x ?? y ?? z;
            Console.WriteLine("Grouped to the left:");
            Baz? b = (x ?? y) ?? z;
            Console.WriteLine("Grouped to the right:");
            Baz? c = x ?? (y ?? z);
        }
    }
    

    Output:

    Unbracketed:
    Foo to Baz
    Grouped to the left:
    Foo to Bar
    Foo to Bar
    Bar to Baz
    Grouped to the right:
    Foo to Baz
    

    In other words,

    x ?? y ?? z
    

    behaves the same as

    x ?? (y ?? z)
    

    but not the same as

    (x ?? y) ?? z
    

    I’m not currently sure why there are two conversions from Foo to Bar when using (x ?? y) ?? z – I need to check that out more carefully…

    EDIT: I now have another question to cover the double conversion…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a null coalescing operator in Javascript? For example, in C#, I can
Is it possible to overload the null-coalescing operator for a class in C#? Say
I know the standard way of using the null coalescing operator in C# is
With the following understanding about null coalescing operator (??) in C#. int? input =
null coalescing translates roughly to return x, unless it is null, in which case
Is there a C++ equivalent for C# null coalescing operator? I am doing too
I'm trying to get the null coallescing operator to work with a LINQ expression
Integer i = null; if (i == 3) Why the second line above throws
I'm trying to use the null-coalescing operator on an int. It works when I
Possible Duplicates: ?? Null Coalescing Operator --> What does coalescing mean? What do 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.