Make a reserved keyword a Variable name in C#

In C# the ‘@‘ character is used to denote literals that explicitly do not adhere to the relevant rules in the language specification.

If we use a keyword as the name for an identifier, we get a compile-time error

"identifier expected, ‘Identifier Name’ is a keyword"

To overcome this error, prefix the identifier with ‘@’.

Such identifiers are verbatim identifiers. The character @ is not actually part of the identifier,

The @ symbol allows you to use a reserved word for variable names. For example:

int @class = 15; // will work 
int class = 15; // won't work

We could have used _class instead, but it will be recorded as _class, that’s what we do not want.

With an ‘@’ symbol, the name is recorded in the assembly as “class” but with an underscore, it is “_class”. If another .NET language doesn’t define “class” as a reserved word, they could use the name just “class”.

 

As per the C# Language Specification:

“The prefix “@” enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier.”