What is the '@' prefix infront of string?

  • Thread starter Thread starter Ann Huxtable
  • Start date Start date
A

Ann Huxtable

Why is it that sometimes strings used in C# are pre-pended by an '@'
character?. Is there any documentation on this (Google searches don't
bring anything useful - "c# + '@' + string variables" etc )
 
The @ sign in front of a string tells the compiler to ignore any embeded
escape sequences.

string "\"" would yield a single double quote.
string "\\" would yield a single back slash
string @"\\" would yield two backslashes
 
Ann Huxtable said:
Why is it that sometimes strings used in C# are pre-pended by an '@'
character?. Is there any documentation on this (Google searches don't
bring anything useful - "c# + '@' + string variables" etc )

The '@' symbol is the verbatim string marker. Basically it means that the
string shouldn't be escaped, that is the escapable characters '\n' for
newline and '\r' for line return, for example, won't be processed.
If written to the console, for example,
"this\nis\na\nline"
would print:
this
is
a
line

@"this\nis\na\nline"
would print:
this\nis\na\nline
 
Why is it that sometimes strings used in C# are pre-pended by an
'@' character?. Is there any documentation on this (Google
searches don't bring anything useful - "c# + '@' + string
variables" etc )

Ann,

In addition to the other responses, putting the @ symbol in front of
a string literal allows you to do things like this:

string pageHtml = @"
<html>
<head>
<title>
My Page
<title>
</head>
<body>
Hello, world!
</body>
</html>";
 
Ann said:
Why is it that sometimes strings used in C# are pre-pended by an '@'
character?. Is there any documentation on this oogle searches don't bring
anything useful - "c# + '@' + string variables" etc )

http://msdn.microsoft.com/library/en-us/csspec/html/vclrfcsharpspec_2_4_4_5.asp

Practically speaking, it is handy if your string contains backslashes or
line breaks, because verbatim string literals may be multiline and the only
character that needs escaping is double quote. On the other hand, if your
string has a lot of double quotes in it, the regular string literal format
with \" escapes may be easier to read.
 
Ann said:
Why is it that sometimes strings used in C# are pre-pended by an '@'
character?. Is there any documentation on this (Google searches don't
bring anything useful - "c# + '@' + string variables" etc )

many thanks all
 
Besides the uses described in other replies, use of @ also allows keywords
to be used as identifiers.

public void (string @string)
{
Console.WriteLine("Value of string is {0}", @string);
}

IMO the fact that you *can* do this does imply that you *should* do it.
 
Besides the uses described in other replies, use of @ also
allows keywords to be used as identifiers.

public void (string @string)
{
Console.WriteLine("Value of string is {0}", @string);
}

IMO the fact that you *can* do this does imply that you *should*
do it.

Ted,

Uhhh... Did you mean "does" or "doesn't"?


Chris.
 
Ted,

Yeah, the C# language specification mentions that this usage of it was
included for cross language support. The idea being that a keyword in
one language doesn't necessarily mean it is in another. Personally,
I've yet to encounter a reason for using it in this manner.

Brian
 
Back
Top