没有@的编译错误
如果我这样做,
movies = new List<string>();
movies.Add("t:\Alistair.flv");
我会在 VS2010 中收到错误
无法识别的转义序列
但是当我这样做时
movies = new List<string>();
movies.Add(@"t:\Alistair.flv");
没有错误,为什么?
If I do this
movies = new List<string>();
movies.Add("t:\Alistair.flv");
I get error in VS2010
Unrecognized escape sequence
However when I do this
movies = new List<string>();
movies.Add(@"t:\Alistair.flv");
There is no error, why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
您的第一个示例:
这里编译器尝试将
\A
解析为转义序列,但无法理解它。第二个
告诉编译器逐字处理字符串,因此忽略任何转义字符。
这个也可以工作,因为 \\ 是退格键的转义字符:
可以找到有关此的技术细节 此处。
Your first example:
Here the compiler tried to parse
\A
as a escape sequence but can't understand it.The second one
Tells the compiler to treat the string verbatim, so ignore any escape characters.
This one will also work, since \\ is the escape char for backspace:
The tehnical details regarding this can be found here.
来自MSDN文档
发生的情况是,当您使用 @ 字符时,编译器会将反斜杠视为反斜杠,而不是像
\t
这样的特殊转义序列的开头。不幸的是,在你的情况下,这种转义序列不存在。From the MSDN documentation
What happens is that when you're using @ character compiler will think backslash as a backslash instead of start of special escape sequence like
\t
. Unfortunately in your case that kind of escape sequence doesn't exist.退格字符“\”表示有特殊字符需要处理。例如。如果你有 \r\n ,它会在字符串中添加一个回车符和新行。
@ 符号基本上告诉它按原样采用反斜杠。
The backspace character, \, indicates that there is a special character to be processed. Eg. If you have \r\n it will add a carriage return and new line to the string.
The @ symbol basically tells it to take the backslash as is.
因为当使用“@”字符时,您告诉编译器按原样获取字符串,因此您必须知道不能包含转义字符。
关于错误,
编译器意味着它不理解
\A
的含义,因为它不是包含以下内容的列表之一:\\ = \
\ n = 换行符
\t = Tab
还有更多,这些称为转义字符,它们不是字面意思,而是有特殊含义。
Because when using `@' character you tell the compiler to take the string as is, you have to know that no escaping characters can be included then.
And about the error
the compiler means it does not understand what you mean by
\A
since it is not one of the list that contain:\\ = \
\n = New line character
\t = Tab
and there is more, these are called escape characters, and they are not taken literally, rather they have special meanings.
由于反斜杠,您会收到错误。您还可以将反斜杠加倍以转义反斜杠:
另请参阅 Jon Skeet 的答案:
Jon Skeet 的这篇文章在这里:
You get the error because of the backslash. You could also double-up the backslash to escape the backslash:
See also Jon Skeet's answer here:
And this article by Jon Skeet here:
尝试使用
此处的信息:在 MSDN 上使用字符串 (参见“反斜杠”)
Try with
Info here : Using Strings on MSDN (see "backward slash")
请参阅此处
http://msdn.microsoft.com/ en-us/library/362314fe(v=vs.71).aspx
\A 被处理为转义序列(\ 标记这一点)。 @ 符号的使用告诉编译器将 \ 字符视为 \ 字符,而不是转义序列的开头。 \A 不是有效的转义序列,因此会出现错误。
See here
http://msdn.microsoft.com/en-us/library/362314fe(v=vs.71).aspx
\A is processed as an escape sequence (the \ marks this). The use of the @ symbol tells the compiler to treat the \ character as a \ character and not the start of an escape sequence. \A is not a valid escape sequence hence the error.