Why do we use @ with “” while trying to replace it with another string
Possible Duplicate:
What's the @ in front of a string in C#?
为什么我们用
字符串文字前面的
因为如果你不这样做,你就必须用
在C中,可以在字符串前面加上
1 | @"" |
相同
1 | "\" |
C语言规范2.4.4.5字符串文字说明:
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.
使用
注意:即使使用逐字模式,
所以我会用它来节省时间,不必通过一个长字符串来转义所有需要转义的字符。
因为反斜杠被视为转义符,如果不使用"@",则会出现"未识别转义序列"错误。使用"@"会告诉编译器忽略转义字符。这可能会有所帮助。