589

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

I understand that the @ symbol can be used before a string literal to change how the compiler parses the string. But what does it mean when a variable name is prefixed with the @ symbol?

Dariusz Woźniak
  • 8,677
  • 5
  • 52
  • 70
Greg
  • 9,783
  • 5
  • 41
  • 64

4 Answers4

748

The @ symbol allows you to use reserved word. For example:

int @class = 15;

The above works, when the below wouldn't:

int class = 15;
Michael Meadows
  • 26,946
  • 4
  • 46
  • 60
  • 36
    With what is it any different than, say, an underscore? – Vilx- Jan 09 '09 at 20:15
  • 109
    With an @ symbol, the name is recorded in the assembly as "class", vs. with an underscore it is "_class". Thus, if another .NET language doesn't define "class" as a reserved word, they could use the name just "class". – P Daddy Jan 09 '09 at 20:23
  • 50
    If you used @class for a property name, you could access it like so: MyClass.class instead of MyClass._class – John Sheehan Jan 09 '09 at 20:24
  • 2
    I think, I could be wrong, in which case, delete my comment – John Sheehan Jan 09 '09 at 20:24
  • John, in Delphi, your comment is accurate. Fully qualified names don't need "@" or "&" escaping. Not sure about C#, though. – Rob Kennedy Jan 09 '09 at 20:49
  • It also shows up as just "class" when using reflection. – Joel Coehoorn Jan 09 '09 at 21:00
  • You shouldn't prefix public members with an underscore as not all .Net languages support it. – Keith Jan 09 '09 at 21:22
  • @Rob: John is correct. @Michael: Nice simple explanation. +1 – Jeff Yates Mar 10 '09 at 19:43
  • 92
    Just when you think you know everything there is to know about C#, you learn something new. :) – Randolpho Dec 09 '09 at 20:19
  • 18
    @Vilx- In ASP.net MVC it's very common to use it because that's the only way to express some things. For example if you want to set an element's class attribute you'd type `new { @class = "mc" };` even tho you meant just "class", that's the only way. The point I'm trying to make is that the `@` is **not** part of the actual name of the variable. – MasterMastic Mar 02 '13 at 13:09
  • 3
    @Ken - Thank you, I got it 4 years ago already. :) – Vilx- Mar 02 '13 at 13:31
  • Contrary to some people's comments, this is quite useful. I had a WSDL which defined an element name as "return" and this allowed me to populate it without reflection (which is what I was contemplating). – JohnOpincar Aug 08 '17 at 18:39
  • Here is microsoft official documentation link about @character - https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/tokens/verbatim – broadband Nov 02 '18 at 08:36
451

The @ symbol serves 2 purposes in C#:

Firstly, it allows you to use a reserved keyword as a variable like this:

int @int = 15;

The second option lets you specify a string without having to escape any characters. For instance the '\' character is an escape character so typically you would need to do this:

var myString = "c:\\myfolder\\myfile.txt"

alternatively you can do this:

var myString = @"c:\myFolder\myfile.txt"
ww1711
  • 5
  • 7
Micah
  • 106,911
  • 83
  • 226
  • 321
  • 34
    How it affects strings was exactly what I was looking for. Thanks! – Scott Mar 09 '11 at 15:24
  • 5
    Thank you, the usage of @ for strings was exactly what I was looking for – Oliver Nov 07 '12 at 10:17
  • 3
    You still need to escape double quotes by doubling them. – Justin Skiles Jan 29 '14 at 19:32
  • If you put @ before a string literal, it is called "verbatim string literal". – Kamran Bigdely Apr 01 '14 at 17:43
  • 1
    MSDN has an article about verbatim string literals (@"hello"): http://msdn.microsoft.com/en-us/library/aa691090%28v=vs.71%29.aspx – Kamran Bigdely Apr 01 '14 at 17:47
  • 5
    This is the better answer IMO – But I'm Not A Wrapper Class Jun 25 '14 at 19:56
  • 5
    @CyberneticTwerkGuruOrc While this is a more detailed answer it goes beyond what the OP asked, the marked answer addresses the specific issue of the @ when used with variables and is therefore (IMO) the more *correct* answer. – Mike Mar 17 '15 at 08:10
  • Note that with Visual Studio 2015 and up, `$"a string"`allows value interpolation. So `string.Format("Your value is {0}.", value)` can now be `$"Your value is \"{value}\"."`. You can combine these to use literal string interpolation, as in `$@"Your value is ""{value}""."`. (Note the difference in escaping the double quote marks in each version.) – ErikE Jan 25 '16 at 23:31
  • This is the correct answer! – Erdinc Ay Sep 06 '17 at 07:40
63

An important point that the other answers forgot, is that "@keyword" is compiled into "keyword" in the CIL.

So if you have a framework that was made in, say, F#, which requires you to define a class with a property named "class", you can actually do it.

It is not that useful in practice, but not having it would prevent C# from some forms of language interop.

I usually see it used not for interop, but to avoid the keyword restrictions (usually on local variable names, where this is the only effect) ie.

private void Foo(){
   int @this = 2;
}

but I would strongly discourage that! Just find another name, even if the 'best' name for the variable is one of the reserved names.

Rasmus Faber
  • 47,301
  • 20
  • 140
  • 187
  • 4
    That's probably good advice. I _think_ that the @ qualifier is the equivalent of VB.Net's square bracket, so the VB equivalent would be: dim [Class] as Int32 = 15 – Michael Meadows Jan 09 '09 at 21:01
  • 4
    @Michael It is exactly the equivalent of VB.NET's square bracket syntax. http://stackoverflow.com/questions/6639688/using-keywords-as-identifiers-in-f notes that F# uses double backticks around an identifier for the same purpose. – ClickRick Dec 21 '14 at 11:14
  • 3
    As a late comment - "not that it is THAT useful" - in MVC that is the way you can pass forward a property named "class" to the render e - which turns into html "class" to define the CSS class. – TomTom Feb 27 '16 at 18:00
15

It allows you to use a C# keyword as a variable. For example:

class MyClass
{
   public string name { get; set; }
   public string @class { get; set; }
}
Joel Coehoorn
  • 380,066
  • 110
  • 546
  • 781