Using “value” as an identifier in C#

后端 未结 6 454
感动是毒
感动是毒 2021-01-03 20:02

In writing short helper functions, I often find myself wanting to use the variable identifier \"value\" as an argument. It seems as though Visual Studio compiles this just f

6条回答
  •  -上瘾入骨i
    2021-01-03 20:46

    C# has lots of contextual keywords. The main reason for them in new versions of the language is to avoid breaking changes with existing compiling code. Contextual keywords lets them add new semantics, without breaking code that was previously valid.

    As mentioned in Eric's article, you can always use @ as a prefix to be able to use a keyword as an identifier. I think the main advantage of that is the ability to interoperate with other libs that may have been developed in other CLR language with a different set of keywords, where a C# keyword (either reserved or contextual) may not be a keyword in that other language.

提交回复
热议问题