20

Possible Duplicate:
when to use @ in c# ?

F.e. string sqlSelect = @"SELECT * FROM Sales".

Thewickedislick
  • 305
  • 1
  • 4
  • 14
Mike
  • 659
  • 2
  • 7
  • 9
  • 2
    Duplicate - http://stackoverflow.com/questions/1057926/when-to-use-in-c – ChrisF Mar 02 '10 at 09:02
  • 6
    Fun fact: you can also use the literal prefix to distinguish an identifier from a keyword. `Int32 new;` is invalid, but `Int32 @new;` is not. – moribvndvs Mar 02 '10 at 09:03

8 Answers8

36

It means interpret the following string as literal. Meaning, the \ in the string will actually be a "\" in the output, rather than having to put "\\" to mean the literal character

Sarfraz
  • 377,238
  • 77
  • 533
  • 578
21

Before string it allows different string formating rules. You can't use backslash to specify special symbols and "" (double quotes become quotes). I find this format very useful for regular expressions

Example

Console.WriteLine(@"\n""\/a"); // outputs \n"\/a 
Console.WriteLine("\\n\"\"\\/a"); // outputs \n"\/a

You might also seen @ symbol before variable. In such case it allows using special C# keywords as variables.

Example:

var @switch = 1;
var @if = "test";
Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108
  • Just stumbled upon this problem and it seems your comment for the second line is wrong. The output should be \n""\/a" - double quotes in the middle. – user258959 Jan 25 '22 at 15:36
  • Is @switch just another variable? Why name it like this, rather than calling something that isn't a C# keyword? – Stewart Mar 08 '22 at 12:15
7

It means there is no need to escape characters in such a string.

So if you want to write the path for c:\Windows, you can write it as

string path = "c:\\Windows";  // Note escaped '\'

OR

string path = @"c:\Windows";  // '\' need not be escaped
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
logicnp
  • 5,796
  • 1
  • 28
  • 32
4

There are two types of string literals, regular and verbatim. The @ symbol makes it a verbatim string literal.

MSDN: String literals (C#)

Community
  • 1
  • 1
Cameron MacFarland
  • 70,676
  • 20
  • 104
  • 133
3

In C and C++, string has some special characters called "escape characters". For example \, & and the " itself is an escape character!

In the very normal way, you to print a statement like:

Nancy Said Hello World! & smiled

you had to set your string like next

string str = "Nancy said Hello World! \& smiled.";

But people in Microsoft made a new cool feature in C# compiler so you can escape the headache of handling the escape characters by adding @ before any string, and the compiler will handle all the escape characters for you by himself. For the last example you can have this in C# like next:

string str = @"Nancy said Hello World! & smiled.";
Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Salem309
  • 56
  • 3
2

Verbatim string literals start with @ and are also enclosed in double quotation marks. For example:

@"good morning"  // a string literal

Nicked from, have a look at the last few lines above the example for more information. http://msdn.microsoft.com/en-us/library/362314fe.aspx

cyberzed
  • 2,026
  • 1
  • 16
  • 26
0

It allows you to have a string with a \ delimiter in it. @"C:\A\b\c\d\e\f" is legal.

Vukašin Manojlović
  • 3,717
  • 3
  • 19
  • 31
Dested
  • 6,294
  • 12
  • 51
  • 73
0

Used for string literal. It marks the string within the quote (") marks as the value without applying any interpretation for symbols in that string.

Kangkan
  • 15,267
  • 10
  • 70
  • 113