The @ in C#

  • Thread starter Thread starter Eric
  • Start date Start date
E

Eric

I saw this line of code

string updateString = @"
update Categories
set CategoryName = 'Other'
where CategoryName = 'Miscellaneous'";

I search for the @ sign in both my C# books and MSDN online but just can't
find it. Can anyone let me know what it is and what is does? Also, why isn't
it part of Microsoft online C# reference?

Thanks,
Eric
 
That means interpret the string literally.

So, if there's a quote or other special character, that is not escaped,
treat it *as* *is*
 
Thank you so much Super Fan.

Super Fan said:
That means interpret the string literally.

So, if there's a quote or other special character, that is not escaped,
treat it *as* *is*
 
Eric said:
I search for the @ sign in both my C# books and MSDN online but just can't
find it. Can anyone let me know what it is and what is does? Also, why isn't
it part of Microsoft online C# reference?
In addition to literal string, as explained by Super Fan, you can put @
in front of an identifier to indicate that the C# compiler should treat
the following word as an identifer. This is useful when you need to use
a reserved word as an identifier. For example:

TypeBuilder @class=module.DefineType("TempClass",TypeAttributes.Public);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
Hah!

I'd been looking for the ability to indicate keywords as identifiers. Coming
from a VB background, I was used to my []. I hunted for this for a while in
C#, and couldn't find it. Thanks a million!

--
Sean Hederman

http://codingsanity.blogspot.com

"Anders Norås [MCAD]" said:
Eric said:
I search for the @ sign in both my C# books and MSDN online but just can't
find it. Can anyone let me know what it is and what is does? Also, why
isn't
it part of Microsoft online C# reference?
In addition to literal string, as explained by Super Fan, you can put @ in
front of an identifier to indicate that the C# compiler should treat the
following word as an identifer. This is useful when you need to use a
reserved word as an identifier. For example:

TypeBuilder @class=module.DefineType("TempClass",TypeAttributes.Public);

Anders Norås
http://dotnetjunkies.com/weblog/anoras/
 
First of all, in the example you give, the @ would have no effect at
all.

Basically, it's means that you can't escape things with \, so if you
want a single backslash, you just use a single backslash. But if you want to
add some other character that needs to be escaped (\n for line feed, \b for
backspace, \x for hex character etc), you can't

Also, if you want a double-quote in the string, it would have to be
doubled ("Hello ""World""" for Hello "World")

--
Truth,
James Curran
[erstwhile VC++ MVP]
Home: www.noveltheory.com Work: www.njtheater.com
Blog: www.honestillusion.com Day Job: www.partsearch.com
 
James Curran said:
First of all, in the example you give, the @ would have no effect at
all.

Yes it would - it would let the code compile.

Try writing the same code - including whitespace - without the @ sign,
and it'll fail because the string literal isn't terminated on the same
line. Verbatim literals can span several lines.
 
Back
Top