In my other question, I found a hack to make this syntax work in MonoDevelop editor:
// hack to make MonoDevelop recognize nameof syntax from C#6.0
using nameof = System.Func<string>;
The C# compilers (Mono and VS) don't give any warnings or errors, and usages of the nameof
keyword also work normally. My question is why.
I'm not a language lawyer but I believe the reason your code works is that
nameof
is a contextual keywordLet's take a step back to a more general case. If you try to create a
using alias directive
for the keywordif
you get an error...… unless you prefix the name with
@
...Similarly you get a CS1003 error if you try to declare a variable of the aliased type ...
… unless you prefix the name with an
@
sign …Contextual keywords on the other hand do not need to be prefixed by
@
…