Take the tour ×

Possible Duplicate:
What's the @ in front of a string in C#?

why do we use @ to replace \ with another string using string.replace(@"\","$$") i'm using C# windows application

share|improve this question
erm, guess what, this came up before at lease once. stackoverflow.com/questions/556133/… –  Jodrell Sep 3 '12 at 14:10
Its also simple to get an answer using google google.co.uk/… –  Jodrell Sep 3 '12 at 14:16
add comment

marked as duplicate by Jodrell, Claudio Redi, Bronumski, Donal Fellows, AVD Sep 4 '12 at 12:28


5 Answers

up vote 6 down vote accepted

The @ in front of a string literal makes it a verbatim string literal, so the backslash \ does not need to be doubled. You can use "\\" instead of @"\" for the same effect.

share|improve this answer
add comment

The C# Language Specification String literals states:

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character), and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences, and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

The verbatim string literal, which uses the @ character, makes it a little easier in practicality to escape almost all the characters that you would otherwise have to escape individually with the \ character in a string.

Note: the " char will still require escaping even with the verbatim mode.

So I would use it to save time from having to go through a long string to escape all the necessary characters that needed escaping.

share|improve this answer
add comment

Because the backslash is treated as an escape character and you would get an 'Unrecognised escape sequence' error if you didn't use '@'. Using '@' tells the compiler to ignore escape characters. this may be helpful.

share|improve this answer
add comment

In C#, you can prefix a string with @ to make it verbatim, so you don't need to escape special characters.


is identical to

share|improve this answer
add comment

Because if you didn't, you'd have to escape \ with \\

@ is used to what's called verbatim strings

share|improve this answer
add comment
