X

What is the @ symbol in front of variables in C#?

dotnet C#

From the C# documentation it states that the @ symbol is an Identifier.
2.4.2 Identifiers

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. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.

So what it really means is it allows you to sue reserved words.
For example


char @int = 'a';
int @class = 47;

//and this will not work
int class = 47;

The other place using @ symbol is when defining strings with escape characters.


string str = "C:\\temp\\folder\\file.txt";
//one can use @ symbol
string str = @"C:\temp\folder\file.txt";

Personally I would not suggest/recommend using the @ symbol as a variable name; naming something string @string just confuses the reader of your code.

Categories: .NET C#
Taswar Bhatti:
Related Post