What's the @ in front of a string in C#?
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"
There is one exception: an escape sequence is needed for the double quote. To escape a double quote, you need to put two double quotes in a row. For instance, @""""
evaluates to "
.
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'";
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.";