The @ symbol in C# code

Posted: (EET/GMT+2)

 

I got an e-mail from a reader of my C# blog entry asking what does the construct "bool @this = true;" mean. Here's an answer.

In C#, there are about 80 keywords, that cannot be used as identifiers. However, by placing the at sign @ in front of an identifier name creates a so-called "verbatim identifier". This construct allows you to use C# keywords as identifier names. As said in the C# Language Specification, section 2.4.2:

"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."

Of course, the other use of the @ sign in C# is the more common verbatim string use. This allows you to prefix string literals with the @ sign, and not espace characters such as backslashes (\). This is very useful for example when working with pathnames.