I am looking into the grammar of C# 5.0 and don't quite understand the use of "base". In the reference manual, there is a notion of "base access" defined as:
base-access:
base . identifier
base [ expression-list ]
Where base
is a keyword, and it appears that this is the only case. However, I encounter C# inputs such as
base.WithAdditionalDiagnostics<TNode>(node, diagnostics);
Can someone point me out which grammar rule this statement refers to? As 'base' appears to be a normal keyword, not contextual, I assume that there should be a specific grammar rule for this case, and base
cannot simply be an identifier.
I believe it should actually be
base-access:
base . identifier type-argument-list_opt
base [ expression-list ]
... which would make it just like member-access:
member-access:
primary-expression . identifier type-argument-list_opt
predefined-type . identifier type-argument-list_opt
qualified-alias-member . identifier type-argument-list_opt
In other words, in an expression
base.WithAdditionalDiagnostics<TNode>(node, diagnostics);
only
base.WithAdditionalDiagnostics<TNode>
is the base-access part - and the rest is parsed as it would be for other calls such as x.WithAdditionalDiagnostics<TNode>(node, diagnostics)
.
From section 7.6.8 of the C# 5 spec:
At binding-time, base-access expressions of the form
base.I
andbase[E]
are evaluated exactly as if they were written((B)this).I
and((B)this)[E]
, whereB
is the base class of the class or struct in which the construct occurs. Thus,base.I
andbase[E]
correspond tothis.I
andthis[E]
, exceptthis
is viewed as an instance of the base class.
Without the additional type-argument-listopt though, I think your existing expression wouldn't parse.
This is actually specified correctly in the 4th edition of the ECMA-334 specification; I shall raise it as a bug with the C# specification (and make sure it doesn't get broken for the 5th edition).
See more on this question at Stackoverflow