168

I've been working with some C# legacy code and I've been seeing a lot of @ symbols in front of variable names. What does this signify or do?

Currently I'm seeing it a lot in front of variables with common names that aren't reserved. E.g.:

MyProcedure(@step.LoadInstanceId, @step.ResultCode, @step.StatusCode);

Given that step isn't a reserved word, is there any reason that they should be escaped?

Orion Adrian
  • 19,053
  • 13
  • 51
  • 67
  • show us some examples. Are they reserved words like @public or just variable names like @weddingDate? – kloucks Oct 31 '08 at 19:47

6 Answers6

229

It's just a way to allow declaring reserved keywords as vars.

void Foo(int @string)
ripper234
  • 222,824
  • 274
  • 634
  • 905
  • 4
    That makes sense, but can you think of any reason why people would be putting them in front of non-keyword variable names -- other than the original developer being stupid? – Orion Adrian Oct 31 '08 at 19:37
  • 1
    My guess is that the previous coder didn't know how they worked. – Orion Adrian Oct 31 '08 at 19:42
  • Maybe he wanted to be sure, like doubly extra sure! In case the variable suddenly turns into a reserved keyword! – Esteban Brenes Oct 31 '08 at 19:43
  • 8
    Maybe he came from a VB background where Step really IS a keyword. – John Rudy Oct 31 '08 at 19:59
  • @ripper: holy crap, why would anyone want to do that? – skaffman Jul 07 '09 at 21:58
  • 10
    No sane coder under normal circumstances. What are some bizzare circumstances? When auto-generating code based on some ruleset, and not wanting to have a special case for reserved words. – ripper234 Jul 07 '09 at 22:03
  • @skaffman: Lots of things generate C# code, like ORM tools. – Scott Stafford Jun 10 '10 at 13:36
  • 6
    Interestingly, you don't need to use the @ can be omitted when using the variable. int @foo = 3; Console.WriteLine(foo); int bar = 4; Console.WriteLine(@bar); – Richard Astbury May 06 '11 at 13:01
  • I actually have seen resharper do this. After refactor/extract method: @group.Properties.PropertyNames – Bluebaron Aug 23 '14 at 18:12
  • I believe it is when a class must mirror some external schema. If the external schema has a field called "class" the mirrored class could call it's property @class. – Jordan Sep 23 '14 at 13:58
  • `can you think of any reason why people would be putting them in front of non-keyword variable names -- other than the original developer being stupid?` - leftovers from automatical rename? – Konrad Morawski Oct 06 '14 at 14:41
  • Another use-case for `@` annotation would be for demonstration purposes in pseudo codes, specially when the code is long enough to confuse. – Rzassar Jul 23 '20 at 11:20
  • Some people must see some internal beauty of this syntax. A co-worker uses a lot of code like this pattern: @__foo . – Louis Sep 09 '20 at 15:36
  • I sometimes use `@this` in factory methods, makes things readable. – Alex from Jitbit Sep 14 '21 at 17:37
40

It allows you to use a reserved word, like 'public' for example, as a variable name.

string @public = "foo";

I would not recommend this, as it can lead to unecessary confusion.

15

Putting @ in front of a string tells the compuler not to process escape sequences found within the string.

From the documentation:

The advantage of @-quoting is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name:

@"c:\Docs\Source\a.txt"  // rather than "c:\\Docs\\Source\\a.txt"

To include a double quotation mark in an @-quoted string, double it:

@"""Ahoy!"" cried the captain." // "Ahoy!" cried the captain.

Another use of the @ symbol is to use referenced (/reference) identifiers that happen to be C# keywords. For more information, see 2.4.2 Identifiers.

Gabe Hollombe
  • 7,847
  • 4
  • 39
  • 44
6

The @ sign is used with identifiers that are the same as language keywords. There are two main uses for this - interoperating with non-C# code. In languages like VB or IronPython the keywords are not the same as in C# and they may have declared classes and properties with names that match C# keywords. If this feature was not present this code would not be accessible in C#. - machine generated code. If a code generator generates code based on some external source (for example WSDL) the generator can just prefix all identifiers with @ and not check and convert identifiers that match C# keywords.

Are you sure that your case is not the second?

Stilgar
  • 22,354
  • 14
  • 64
  • 101
5

This escapes reserved words in C#.

Jacob Carpenter
  • 4,134
  • 1
  • 29
  • 30
3

The original question asks for a reason why one would escape a not-reserved word. What comes to my mind is that if step would become a reserved word in future the code example would still compile. I guess it is also a valid option for code generators.

mklein
  • 1,787
  • 1
  • 15
  • 18