What is the purpose of the @ Character?

  • Thread starter Thread starter Mick Walker
  • Start date Start date
M

Mick Walker

Hi All,

I have seen the '@' character used with strings on several occasions.
Comming from a vb background I am not sure what this actually does.

i.e:
string strScript = @'alert(document.forms[0]['Textbox1'].value);";

What is the purpose of the '@' character in this line, because as far as
I can tell it works fine with or without it?
 
Hi Mick

It removes the need to use double backslashes in literal strings

@"c:\documents and settings\my docs"

is the same as

"c:\\documents and settings\\my docs"

HTH
 
Mick said:
Hi All,

I have seen the '@' character used with strings on several occasions.
Comming from a vb background I am not sure what this actually does.

i.e:
string strScript = @'alert(document.forms[0]['Textbox1'].value);";

What is the purpose of the '@' character in this line, because as far as
I can tell it works fine with or without it?

It signifies a literal string...
The backslash \ is used for special characters \n = carriage return etc.
but putting the @ sign means that you can pass a literal path.
 
Mick said:
What is the purpose of the '@' character in this line, because as far
as I can tell it works fine with or without it?

It makes the string literal because suppresses the escape character (\) as
in:

string Path1 = "C:\\Temp"; // Double backslash needed to get a single
backslash in the string

string path2 = @"C:\Temp";

This also means that you cannot use the quote character (") in strings
prepended with @.

Ebbe
 
It makes the string literal because suppresses the escape character (\) as
in:

<snip>

Just to get the terminology correct, it indicates a *verbatim* string
literal.

"Hello" is still a string literal - but @"Hello" is a verbatim string
literal.

To the OP: See http://pobox.com/~skeet/csharp/strings.html for this
and more info on strings, escape characters etc.

Jon
 
Mick said:
Hi All,

I have seen the '@' character used with strings on several occasions.
Comming from a vb background I am not sure what this actually does.

i.e:
string strScript = @'alert(document.forms[0]['Textbox1'].value);";

What is the purpose of the '@' character in this line, because as far as
I can tell it works fine with or without it?
I believe that the "'" after the "@" should be '"'. If you see what I
mean! As for what it *means* others have covered that.

Cheers,

Cliff
 
Another use of @ symbol is to use referenced identifiers that happen to be
C# keywords.
e.g. you can declare a string variable with name '@string':
string @string = "string value"; // here put @ ahead of 'string' means it is
referenced as an identifier not the keyword
 
Laser said:
Another use of @ symbol is to use referenced identifiers that happen to be
C# keywords.
e.g. you can declare a string variable with name '@string':
string @string = "string value"; // here put @ ahead of 'string' means it is
referenced as an identifier not the keyword

Indeed. Although if I ever see such code in a codebase I'm working on,
outside autogenerated code, I'll be having serious words with the
author :)

Jon
 
Jon said:
Indeed. Although if I ever see such code in a codebase I'm working on,
outside autogenerated code, I'll be having serious words with the
author :)

I saw this line the other day, and it made my head hurt:

string @bool = "true";

I think it could do people a lot of favours to just suddenly remove that
particular use of @.

Chris.
 
"This also means that you cannot use the quote character (") in strings
prepended with @."

That's incorrect. You can use quotes within a verbatim literal string if
you delimit them with another quote.
--
David Anton
http://www.tangiblesoftwaresolutions.com
Convert between VB, C#, C++, and Java
Instant C#
Instant VB
Instant C++
C++ to C# Converter
C++ to VB Converter
C++ to Java Converter
 
I saw this line the other day, and it made my head hurt:

string @bool = "true";

I think it could do people a lot of favours to just suddenly remove that
particular use of @.

No, you've got to keep it, otherwise you can't use members/classes
written in other languages which are exposed but using an existing
keyword as their name.

Yes, it's something which should be avoided wherever possible - but
you need to be able to cope with it if it happens.

Jon
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top