2

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?

Community
  • 1
  • 1
Jamie
  • 988
  • 1
  • 11
  • 19

3 Answers3

4

It lets you name a variable using a reserved keyword.

For example:

var @class = "something"; // OK
var class = "something"; // Compilation error
Oded
  • 489,969
  • 99
  • 883
  • 1,009
0

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

TimC
  • 1,051
  • 9
  • 16
  • 1
    The @ is not part of the name, and thus does not add any uniqueness to it. In the example given it is superfluous. ReSharper outputs them (correctly) if the name is a reserved word. – R. Martinho Fernandes Jan 18 '11 at 16:43
0

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; 
James
  • 5,355
  • 2
  • 18
  • 30