How the right associative of null coalescing operator behaves?

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...


Jon's answer is correct.

Just to be clear: the ?? operator in C# is right associative. I have just gone through the binary operator parser and verified that the parser treats ?? as right-associative.

As Jon points out, the spec says both that the ?? operator is right-associative, and that all binary operators except assignment are left-associative. Since the spec contradicts itself, clearly only one of these can be right. I will have the spec amended to say something like:

Except for the simple assignment, compound assignment and null coalescing operators, all binary operators are left-associative

UPDATE: As noted in the comments, the lambda operator => is also right-associative.


I can't see how it matters, both:

(a ?? b) ?? c

and

a ?? (b ?? c)

have the same result!


Both work as expected and are effectively identical because the expressions involve simple types (thank you @Jon Skeet). It will chain to the first non-null from left to right in your examples.

The precedence (thanks @Ben Voigt) is more interesting when combining this operator with operators of different precedence:

value = A ?? B ? C : D ?? E;

Basically, associativity is expressed inherently through operator precedence or through user introduced sub-expressions (parentheses).

Tags:

C#