145

Possible Duplicate:
What's the @ in front of a string for .NET?

I found this in a C# study book

DirectoryInfo dir = new DirectoryInfo(key.Key.ToString() + @":\");

The book however did not explain what the '@' symbol was for. I tried searching MSDN C# Operators but its not listed there. I can guess that it allows the developer to not have to escape a '\' or does it allow to not have any escape sequences?

What is this for and why would I use @":\" instead of ":\\"?

Thanks for the help

Edit: See the comment below for a similar question

Community
  • 1
  • 1
Daniel
  • 2,762
  • 3
  • 25
  • 24
  • 1
    can I point out how counter productive it is to close as duplicates and list the names of the people who felt that way but not a link to the duplicated post? You have done nothing to prevent Google from directing people to this page which you do not allow answers too, and provide no link... – Dan Sep 26 '18 at 20:14
  • 1
    @Dan the link to possible duplicates is at the top under "Possible Duplicate" and on the right under "Linked" – Daniel Oct 04 '18 at 00:18

5 Answers5

232

It means to interpret the string literally (that is, you cannot escape any characters within the string if you use the @ prefix). It enhances readability in cases where it can be used.

For example, if you were working with a UNC path, this:

@"\\servername\share\folder"

is nicer than this:

"\\\\servername\\share\\folder"
Mark Avenius
  • 13,679
  • 6
  • 42
  • 50
  • 19
    Well you can escape " by doubling them up i.e. `string S = @""""; Console.Write("[{0}]", S);` writes `[""]` – Binary Worrier Feb 02 '11 at 19:57
  • @Mark So any escape sequence in the string would be ignored and treated at literal text? – Daniel Feb 02 '11 at 20:07
  • 2
    @Daniel: correct; any sequence that would otherwise be backslash-escaped will be treated literally. http://msdn.microsoft.com/en-us/library/362314fe(v=VS.100).aspx explains further and gives examples. – Mark Avenius Feb 02 '11 at 20:12
  • @Mark that link was exactly what I needed - thanks – Daniel Feb 02 '11 at 20:23
  • 1
    +1, I never realized you can double " to escape it. I always hated not being able to escape " when using @. I feel dumb now. Thanks... – Dustin Davis Feb 02 '11 at 21:59
84

It also means you can use reserved words as variable names

say you want a class named class, since class is a reserved word, you can instead call your class class:

IList<Student> @class = new List<Student>();
Neil N
  • 24,862
  • 16
  • 85
  • 145
26

Prefixing the string with an @ indicates that it should be treated as a literal, i.e. no escaping.

For example if your string contains a path you would typically do this:

string path = "c:\\mypath\\to\\myfile.txt";

The @ allows you to do this:

string path = @"c:\mypath\to\myfile.txt";

Notice the lack of double slashes (escaping)

MrEyes
  • 13,059
  • 10
  • 48
  • 68
10

As a side note, you also should keep in mind that "escaping" means "using the back-slash as an indicator for special characters". You can put an end of line in a string doing that, for instance:

String foo = "Hello\

There";
Doodloo
  • 869
  • 5
  • 18
2

What is this for and why would I use @":\" instead of ":\"?

Because when you have a long string with many \ you don't need to escape them all and the \n, \r and \f won't work too.

Sairam
  • 375
  • 1
  • 5
  • 28
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928