471,612 Members | 2,515 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,612 software developers and data experts.

What is the '@' prefix infront of string?

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 )

Nov 26 '05 #1
10 17753
It represents a verbatim string literal.
http://msdn.microsoft.com/library/de...ec_2_4_4_5.asp

--
Tim Wilson
..NET Compact Framework MVP

"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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 )

Nov 26 '05 #2
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

--

Andrew Robinson
www.binaryocean.com
www.bellinghamdotnet.org
"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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 )

Nov 26 '05 #3

"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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
Nov 26 '05 #4
Ann Huxtable <an**********@research-labs.de> wrote in
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com:
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>";
--
Hope this helps.

Chris.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Nov 26 '05 #5


Ann Huxtable wrote:
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

Nov 26 '05 #6
Ann Huxtable wrote:
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...ec_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.

--
Chris Priede
Nov 26 '05 #7
Ann Huxtable <an**********@research-labs.de> wrote:
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 )


See http://www.pobox.com/~skeet/csharp/s....html#literals

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Nov 26 '05 #8
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.

"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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 )

Dec 4 '05 #9
"Ted Miller" <te*@millerzone.net> wrote in
news:11*************@corp.supernews.com:
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.
-------------
C.R. Timmons Consulting, Inc.
http://www.crtimmonsinc.com/
Dec 4 '05 #10
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

Ted Miller wrote:
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.

"Ann Huxtable" <an**********@research-labs.de> wrote in message
news:dm**********@nwrdmz01.dmz.ncs.ea.ibs-infra.bt.com...
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 )


Dec 4 '05 #11

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by hlddn | last post: by
2 posts views Thread by Supra | last post: by
1 post views Thread by tggfang | last post: by
6 posts views Thread by Eric Moors | last post: by
reply views Thread by Christian Heimes | last post: by
reply views Thread by leo001 | last post: by
1 post views Thread by ZEDKYRIE | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.