0

Possible Duplicate:
What's the @ in front of a string in C#?

This is something I have questioned for a long time but never bothered to figure out. When I download third party libraries I have often seen string assignments using a @ symbol before the string.

string myString = @"Some text";

But there seems to be absolutely no difference if I simply do

string myString = "Some text";

So what is the @ doing?

Community
  • 1
  • 1
Michael Mankus
  • 4,628
  • 9
  • 37
  • 63
  • Sorry, I didn't see that when I was searching. – Michael Mankus Sep 13 '12 at 18:34
  • 1
    @michael.mankus well how much "research" did you do then? I remember looking for it when I didn't know what it meant and finding it in seconds... – MarioDS Sep 13 '12 at 18:35
  • In all fairness, I did a google search (https://www.google.com/#hl=en&sclient=psy-ab&q=what+does+the+%40+do+in+c%23) and none of the links on the first page of results even address the issue. I thought it seemed to be a fair enough question. But if you feel the need to downvote, it's okay. And as I said, I am sorry for the duplicate. – Michael Mankus Sep 13 '12 at 18:41
  • 1
    I didn't downvote you, but I might say that your Google search wasn't well written. The @ in C# can be anything, if you simply add the word "string" the result pops up very quickly. – MarioDS Sep 13 '12 at 18:43

4 Answers4

7

It is signifies a verbatim string literal and allows you to not have to escape certain characters:

string foo = @"c:\some\path\filename.exe";

vs:

string foo = "c:\\some\\path\\filename.exe";
Alex
  • 34,899
  • 5
  • 77
  • 90
2
string reason = @"this string literal mea\ns something different with an @ in front than without";

Without the @, the above string would have a new-line character instead of an 'n' in the word "means". With the @, the word "means" looks just like you see it. This feature is especially useful for things like file paths:

string path = @"C:\Users\LookMa\NoDoubleSlashes.txt";
Joel Coehoorn
  • 399,467
  • 113
  • 570
  • 794
1

It is a verbatim string literal. It lets you do things like @"C:\" instead of "C:\\", and is especially useful in regex and file paths, since these often use backslashes that shouldn't be parsed by the compiler. See the documentation for more info.

Tim S.
  • 55,448
  • 7
  • 96
  • 122
0

In this case there is no difference. All '@' does is allow you to omit escape backslashes. If you use '@' and want in include double quotes in the string you need to double up the double quotes.

Dave Doknjas
  • 6,394
  • 1
  • 15
  • 28