3

Possible Duplicate:
What's the use/meaning of the @ character in variable names in C#?

i have seen in several places including Resharper converted code ...

SomeClass @object = new SomeClass( ... );

I thought it is anonymous, but i think i am wrong

Does it really mean something?

Community
  • 1
  • 1
guyl
  • 2,158
  • 4
  • 32
  • 58

6 Answers6

8

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.

Example:

class @class
{
   public static void @static(bool @bool) {
      if (@bool)
         System.Console.WriteLine("true");
      else
         System.Console.WriteLine("false");
   }   
}

See: http://msdn.microsoft.com/en-us/library/aa664670.aspx

James Hill
  • 60,353
  • 20
  • 145
  • 161
2

It allows you to use reserved keywords as identifiers. For example, a common scenario I run into this is when defining locking objects e.g.

object lock = new object(); // this would throw a compiler error as `lock` is a reserved word.

So I have to prefix it with the @ symbol to tell the compiler this isn't a type but a literal:

object @lock = new object();

On a side note, it's normally a good indicator that you aren't descriptive enough with your indentifiers if you are having clashes like this.

James
  • 80,725
  • 18
  • 167
  • 237
1

The reason behind this is that object is a protected keyword in C#. The @ sign will tell the compiler that that should be the name of the variable, and should not be confused with the objectkeyword.

GeirGrusom
  • 999
  • 5
  • 18
0

It means "literal", i.e. "this name is not a keyword or type identifier". Without it, object would be treated as a type.

Polynomial
  • 27,674
  • 12
  • 80
  • 107
0

See my answer in this question.

The @ symbol allows you to use reserved words in a variable name.

int @class = 1;

void MyMethod(int @goto);

bool @public { get; set; }
Community
  • 1
  • 1
Connell
  • 13,925
  • 11
  • 59
  • 92
  • I would disagree that Resharper is *wrong*. It's simply flagging it up as it's probably not the best approach to be naming variables after **any** compiler keywords (regardless of reserved/contextual). – James Nov 14 '11 at 12:40
  • I'd agree that it isnt the best approach to name variables after any compiler keyword at all, but I'd argue that if it wasn't the best approach, let it error out, not try to fix all of them, especially the one's that don't even cause any errors! ;) – Connell Nov 14 '11 at 12:44
0

Keywords are predefined reserved identifiers that have special meanings to the compiler. They cannot be used as identifiers in your program unless they include @ as a prefix. For example, @if is a legal identifier but if is not because it is a keyword.

Refer this C# Keywords

Siva Charan
  • 17,940
  • 9
  • 60
  • 95