5

Possible Duplicate:
what does “@” means in c#

What does the sign @ mean in the following:

Class.Field = @"your text here";

I came across this in a piece of code, the compiler does not seem to complain... I've searched around to no avail...

What does the @ mean?

Community
  • 1
  • 1
unom
  • 11,438
  • 4
  • 34
  • 54
  • You will find more info here: http://msdn.microsoft.com/en-us/library/362314fe.aspx – codaddict Oct 07 '10 at 18:20
  • 5
    For your future reference the name of this feature is "verbatim string literals". That should help you do web searches for it, and so on. – Eric Lippert Oct 07 '10 at 18:20

4 Answers4

9

It indicates a verbatim string literal. You can use it so escapes aren't treated as such:

string path = "C:\\Documents and Settings\\UserName\\My Documents";

Becomes:

string path = @"C:\Documents and Settings\UserName\My Documents";
Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
4

You can do something like

@"C:\temp\testfile.txt"

Without @ you would need to do

"C:\\temp\\testfile.txt"

Also it helps you work with other more complicated strings that should represent XML, for example.

Only one thing that you would need to double-write is " itself.

So, @"Tom said ""Hello!""";

Andriy Buday
  • 1,959
  • 1
  • 17
  • 40
0

It a short hand so that \ isn't needed to escape the backslash.

In regular expressions, it is very useful because Regex relies so heavily on the backslash character. When using the @ sign, the only character that you need to escape is the double quote, and it is escaped with a second double quote (a al VB). All other characters are literal.

(Directory paths are another form of string that benefits from the @)

In your example, the @ is unnecessary.

Les
  • 10,335
  • 4
  • 40
  • 60
0

It tells the compiler not to consider the backslash character as an escape sequence.

Robaticus
  • 22,857
  • 5
  • 54
  • 63