What does '@' at beginning of a variable mean? [duplicate]
开发者_Go百科Possible Duplicate:
What does the @ symbol before a variable name mean in C#?
I've seen this a couple of times in code that has been passed onto me:
try {
//Do some stuff
}
catch(Exception @exception)
{
//Do catch stuff
}
Can anyone please explain the purpose of the '@' at the beginning of the Exception variable?
It lets you name a variable using a reserved keyword.
For example:
var @class = "something"; // OK
var class = "something"; // Compilation error
Resharper outputs them sometimes if the name of the variable is close to a class name or a namespace i believe, it is just giving it a unique non clashing name
Shameless rip of Michael Meadows answer to a duplicate question follows.
The @ symbol allows you to use reserved word. For example:
int @class = 15;
The above works, when the below wouldn't:
int class = 15;
精彩评论