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
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
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.
Because if you didn't, you'd have to escape \
with \\
@
is used to what's called verbatim strings
In C#, you can prefix a string with @
to make it verbatim, so you don't need to escape special characters.
@"\"
is identical to
"\\"
The C# Language Specification 2.4.4.5 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.