In the following code, one of the two variants does not compile:
class C
{
public decimal DecimalField;
}
static C GetC() { return new C(); } //Can return null in reality.
C c = GetC(); //Get a C value from somewhere, this might be null
string toString1 = c?.DecimalField?.ToString(); //Does not compile.
string toString2 = (c?.DecimalField)?.ToString(); //Compiles.
Error CS0023: Operator '?' cannot be applied to operand of type 'decimal'
Why does the simple form not compile?
Wouldn't the expression c?.DecimalField be of type decimal?? That value can be null so the ?. operator should apply. I'm quite sure this is so because in this code:
var decimalValue = c?.DecimalField;
the var does resolve to decimal? according to the IDE.
decimalis not nullablethe simple form, adding the parenthesis changes the expression?.and (to my mind) it's difficult to know what associativity would even mean here.