开发者

What does '@' at beginning of a variable mean? [duplicate]

This question already has answers here: Closed 12 years ago.

Possible Duplicate:

What does the @ symbol before a variable name mean in C#?

开发者_Go百科

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; 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜