tags:

views:

1186

answers:

8

This is a .NET question for C# (or possibly VB.net), but I am trying to figure out what's the difference between the following declarations:

string hello = "hello";

vs.

string hello_alias = @"hello";

Printing out on the console makes no difference, the length properties are the same.

Thanks!

+16  A: 

It's a verbatim string literal. It means that escaping isn't applied. For instance:

string verbatim = @"foo\bar";
string regular = "foo\\bar";

Here verbatim and regular have the same contents.

It also allows multi-line contents - which can be very handy for SQL:

    string select = @"
SELECT Foo
FROM Bar
WHERE Name='Baz'";

(Not that you should have SQL in code very often, of course :)

The one bit of escaping which is necessary for verbatim string literals is to get a double quote (") which you do by doubling it:

string verbatim = @"He said, ""Would you like some coffee?"" and left.";
string regular = "He said, \"Would you like some coffee?\" and left.";
Jon Skeet
Better add the "@"...
Joe R
Doh! Fixed, thanks.
Jon Skeet
still seems to be missing a couple of "@"s and an extra quote from the final verbatim string literal
Mark Heath
Looks like edg fixed the missing quote, but I can't see anywhere that's missing an @...
Jon Skeet
usually I wouldn't dare question Jon Skeet, but "Note that you *should* have SQL in code very often, of course"????
Jon
That's better. :)
Joe R
(I fixed more than the missing quote, but edit history doesn't show it) (I guess it's "smart" history in keeping with the fine traditions of "smart" features)
Ed Guiness
Goodo :) Apologies for getting it wrong to start with. Doh 'n all that :) That's the problem with writing/editing while distracted...
Jon Skeet
The SQL bit was actually a typo at the start: it should have read "Not that you should have" (as it now does).
Jon Skeet
It also allows using reserved words as variable names and stuff. Like public int GetValueOrDefault(int @default);
Svish
Svish: True but unrelated to this specific question.
Jon Skeet
I didn't know about the double quote escape. Live and learn thanks
ShuggyCoUk
Upvoted before I'd realised it was Jon, who doesn't need any more upvotes!
Chris Needham
@Chris: On the other hand, an upvote now doesn't make any difference to my rep, so no harm done :)
Jon Skeet
OK - kinda spooky that we both forgot the darned @s... cheers for fixing.
Marc Gravell
+29  A: 

It marks the string as a verbatim string literal - anything in the string that would normally be interpreted as an escape sequence is ignored.

So "C:\\Users\\Rich" is the same as @"C:\Users\Rich"

Richard Ev
"" and @"" are both string literals, but the latter is a *verbatim* string literal
Ed Guiness
+1 for edg's comment.
Jon Skeet
Thanks for the correction edg - I have updated my answer
Richard Ev
Don't forget the one escape sequence needed when using verbatim strings: the double quote. You need double double quotes. Like this: @"""" => ".
Martinho Fernandes
+1 for Martinho's comment
Richard Szalay
+4  A: 

http://msdn.microsoft.com/en-us/library/aa691090.aspx

C# supports two forms of string literals: regular string literals and verbatim string literals.

A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.

A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.

Ed Guiness
+1  A: 

This is a verbatim string, and changes the escaping rules - the only character that is now escaped is ", escaped to "". This is especially useful for file paths and regex:

var path = @"c:\some\location";
var tsql = @"SELECT *
            FROM FOO
            WHERE Bar = 1";
var escaped = @"a "" b";

etc

Marc Gravell
Aren't you missing the @ in you example, Marc? Or is it the default when I use var? Little confused...
0xA3
Marc was just emulating my earlier errors :)
Jon Skeet
Thanks for correcting ;)
0xA3
OK - that is really, really odd. I wonder if the editor munched them?
Marc Gravell
A: 

Putting a @ infront of a string enables you to use special characters such as a backslash or double-quotes without having to use special codes or escape characters.

So you can write:

string path = @"C:\My path\";

instead of:

string path = "C:\\My path\\";

Presidenten
+1  A: 

Copied from MSDN:

At compile time, verbatim strings are converted to ordinary strings with all the same escape sequences. Therefore, if you view a verbatim string in the debugger watch window, you will see the escape characters that were added by the compiler, not the verbatim version from your source code. For example, the verbatim string @”C:\files.txt” will appear in the watch window as “C:\files.txt”.

aanund
+2  A: 

Since you explicitly asked for VB as well, let me just add that this verbatim string syntax doesn't exist in VB, only in C#. Rather, all strings are verbatim in VB. I.e.:

Dim path = "C:\My\Path"
Dim message = "She said, ""Hello, beautiful world."""

Escape sequences don't exist in VB (except for the doubling of the quote character, like in C# verbatim strings) which makes a few things more complicated. For example, to write the following code in VB you need to use concatenation (or any of the other ways to construct a string.

string x = "Foo\nbar";

in VB would be written like this:

Dim x = "Foo" & Environment.NewLine & "bar"

(& is the VB string concatenation operator. + could equally be used.)

Konrad Rudolph
Oh my, that sounds annoying... even more glad I'm using C# now :p
Svish
A: 

An '@' has another meaning as well: putting it in front of a variable declaration allows you to use reserved keywords as variable names.

For example:

string @class = "something";
int @object = 1;

I've only found one or two legitimate uses for this. Mainly in ASP.NET MVC when you want to do something like this:

<%= Html.ActionLink("Text", "Action", "Controller", null, new { @class = "some_css_class" })%>

Which would produce an HTML link like:

<a href="/Controller/Action" class="some_css_class">Text</a>

Otherwise you would have to use 'Class', which isn't a reserved keyword but the uppercase 'C' does not follow HTML standards and just doesn't look right.

JulianR