In the C# specification is said:
Except for the assignment operators and the null coalescing operator, all binary operators are left-associative, meaning that operations are performed from left to right.
The assignment operators, the null coalescing operator and the conditional operator (?:) are right-associative, meaning that operations are performed from right to left.
that's enough clear but operators like default
, delegate
, stackalloc
, await
how they are classified? Which is their associativity?
The same can be said for []
, ()
, e.g; are they unary or binary or technically none of them because according the standard unary operator is something like op x while binary operator is something like x op y?
There's no problem here, because all the non-primary unary operators have the same precedence as each other, and a different precedence to all binary operators. Associativity comes into effect when an operand is between two operators of the same precedence - but an operand can't be between two non-primary unary operators.
All the primary unary operators (new
, typeof
, default
, checked
, unchecked
and delegate
) have syntax rules which mean they're not a problem - basically, you can tell the operand by where the parenthese/braces are. Without the parentheses, there could be a problem. For example, supposed the unchecked
operator didn't require parentheses. Then this:
unchecked x . y
could mean unchecked(x.y)
or (unchecked x).y
without more rules. It's not a problem though, as it's not valid anyway.
It's worth noting that the precedence and associativity explanations in the specification are really only informational anyway - the precise rules are encoded in the grammar of the language already. In other words, the section could be removed from the spec without affecting the validity or meaning of any program. (Good job too, as there are a couple of mistakes, IIRC...)
See more on this question at Stackoverflow