开发者

Compilation error without the @

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开发者_JAVA技巧 = new List<string>();
movies.Add(@"t:\Alistair.flv");

There is no error, why?


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.


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.


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.


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.


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:

  • What does an @ before the start of a string literal mean?

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:

  • Strings in C# and .NET


Try with

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

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜