4

Possible Duplicate:
What's the @ in front of a string for .NET?

Sometimes i saw the sample code, will have a "@" symbol along with the string. for example:

    EntityConnectionStringBuilder entityBuilder = new EntityConnectionStringBuilder();
    entityBuilder.Provider = "System.Data.SqlServerCe.3.5";
    entityBuilder.ProviderConnectionString = providerString;
    entityBuilder.Metadata = @"res://*/App_Data.data.csdl|res://*/App_Data.data.ssdl|res://*/App_Data.data.msl";

On the 4th line, what is that usage of the "@"? I try to remove this, it still works.

Community
  • 1
  • 1
Cheung
  • 15,293
  • 19
  • 63
  • 93

2 Answers2

4

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.

Taken from

Kimtho6
  • 6,154
  • 9
  • 40
  • 56
  • (while external links are useful, they tend to disappear unpredictably; having some content in the actual post is preferred) – Marc Gravell Dec 02 '10 at 14:19
1

It tells the compiler not to treat \ as escape sequences and take the strings literally.

bevacqua
  • 47,502
  • 56
  • 171
  • 285