没有@的编译错误

发布于 2024-10-15 09:00:22 字数 306 浏览 5 评论 0原文

如果我这样做,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

拥抱我好吗 2024-10-22 09:00:22

您的第一个示例:

"t:\Alistair.flv"

这里编译器尝试将 \A 解析为转义序列,但无法理解它。

第二个

@"t:\Alistair.flv"

告诉编译器逐字处理字符串,因此忽略任何转义字符。

这个也可以工作,因为 \\ 是退格键的转义字符:

"t:\\Alistair.flv"

可以找到有关此的技术细节 此处

Your first example:

"t:\Alistair.flv"

Here the compiler tried to parse \A as a escape sequence but can't understand it.

The second one

@"t:\Alistair.flv"

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:

"t:\\Alistair.flv"

The tehnical details regarding this can be found here.

屋顶上的小猫咪 2024-10-22 09:00:22

来自MSDN文档

@符号告诉字符串
构造函数忽略转义
字符和换行符

发生的情况是,当您使用 @ 字符时,编译器会将反斜杠视为反斜杠,而不是像 \t 这样的特殊转义序列的开头。不幸的是,在你的情况下,这种转义序列不存在。

From the MSDN documentation

The @ symbol tells the string
constructor to ignore escape
characters and line breaks

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.

自在安然 2024-10-22 09:00:22

退格字符“\”表示有特殊字符需要处理。例如。如果你有 \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.

三月梨花 2024-10-22 09:00:22

因为当使用“@”字符时,您告诉编译器按原样获取字符串,因此您必须知道不能包含转义字符。

关于错误,

Unrecognized escape sequence

编译器意味着它不理解 \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

Unrecognized escape sequence

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.

我是男神闪亮亮 2024-10-22 09:00:22

由于反斜杠,您会收到错误。您还可以将反斜杠加倍以转义反斜杠:

movies.Add("t:\\Alistair.flv");

另请参阅 Jon Skeet 的答案:

诸如@"c:\Foo"之类的字符串文字称为逐字字符串文字。它基本上意味着“在到达下一个引号字符之前不要对字符应用任何解释”。因此,逐字字符串文字可以包含反斜杠(无需将它们折叠起来)甚至行分隔符。要在逐字文字中获得双引号 ("),只需将其加倍,例如 @"My name is ""Jon""" 表示字符串 My name is "Jon"。包含行分隔符的逐字字符串文字还将在行的开头包含空格,因此在空格很重要的情况下我倾向于不使用它们,但它们对于在源代码中包含 XML 或 SQL 非常方便,也是另一种典型用途。 (不需要行分隔符)用于指定文件系统路径。

Jon Skeet 的这篇文章在这里:

You get the error because of the backslash. You could also double-up the backslash to escape the backslash:

movies.Add("t:\\Alistair.flv");

See also Jon Skeet's answer here:

A string literal such as @"c:\Foo" is called a verbatim string literal. It basically means, "don't apply any interpretations to characters until the next quote character is reached". So, a verbatim string literal can contain backslashes (without them being doubled-up) and even line separators. To get a double-quote (") within a verbatim literal, you need to just double it, e.g. @"My name is ""Jon""" represents the string My name is "Jon". Verbatim string literals which contain line separators will also contain the white-space at the start of the line, so I tend not to use them in cases where the white-space matters. They're very handy for including XML or SQL in your source code though, and another typical use (which doesn't need line separators) is for specifying a file system path.

And this article by Jon Skeet here:

翻身的咸鱼 2024-10-22 09:00:22

尝试使用

movies.Add("t:\\Alistair.flv");

此处的信息:在 MSDN 上使用字符串 (参见“反斜杠”)

Try with

movies.Add("t:\\Alistair.flv");

Info here : Using Strings on MSDN (see "backward slash")

惜醉颜 2024-10-22 09:00:22

请参阅此处

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文