153

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

2
  • 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... Commented Sep 26, 2018 at 20:14
  • 1
    @Dan the link to possible duplicates is at the top under "Possible Duplicate" and on the right under "Linked" Commented Oct 4, 2018 at 0:18

5 Answers 5

246

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"
Sign up to request clarification or add additional context in comments.

5 Comments

Well you can escape " by doubling them up i.e. string S = @""""; Console.Write("[{0}]", S); writes [""]
@Mark So any escape sequence in the string would be ignored and treated at literal text?
@Daniel: correct; any sequence that would otherwise be backslash-escaped will be treated literally. msdn.microsoft.com/en-us/library/362314fe(v=VS.100).aspx explains further and gives examples.
+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...
@BinaryWorrier shouldn't it write ["] (with one double quote) instead? See the answer in the original question: stackoverflow.com/questions/556133/…
88

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>();

2 Comments

Thats pretty cool - I didn't know you could do that. Thanks
True but off topic.
28

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)

1 Comment

Thanks, and from what I read, it creates unescapable strings.
11

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";

1 Comment

+1 I always though the newline was exclusively to @"... style
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.

1 Comment

pardon me, there's a misspell in "string" word.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.