Connecting Tech Pros Worldwide Forums | Help | Site Map

The @ symbol in front of some strings

Water Cooler v2
Guest
 
Posts: n/a
#1: Nov 17 '05
I see this @ character a lot in front of C# strings. I searched the
documentation online but coult not come up with the page that explains
this symbol. What is it used for?


carion1
Guest
 
Posts: n/a
#2: Nov 17 '05

re: The @ symbol in front of some strings


A string prefixed with @ means the escape sequences do not get processed.

localPath = "c:\\test\\test.txt";

is the same as

localPath = @"c:\test\test.txt";

"Water Cooler v2" wrote:
[color=blue]
> I see this @ character a lot in front of C# strings. I searched the
> documentation online but coult not come up with the page that explains
> this symbol. What is it used for?
>
>[/color]
Water Cooler v2
Guest
 
Posts: n/a
#3: Nov 17 '05

re: The @ symbol in front of some strings


Thanks.

Jon Skeet [C# MVP]
Guest
 
Posts: n/a
#4: Nov 17 '05

re: The @ symbol in front of some strings


Water Cooler v2 <wtr_clr@yahoo.com> wrote:[color=blue]
> I see this @ character a lot in front of C# strings. I searched the
> documentation online but coult not come up with the page that explains
> this symbol. What is it used for?[/color]

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

--
Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Ted Miller
Guest
 
Posts: n/a
#5: Nov 17 '05

re: The @ symbol in front of some strings


The @ can also be used to escape a keyword so you can use it as an
identifier.

public void MyMethod(string @string) // You now have a variable
called string
{
//...
}

Now, whether you should ever actually do this is debatable at best. It's
useful for things like machine-generated code, where you might be generating
a class where parameter names are derived from a source that doesn't have
the same keywords as C# (i.e., xsd.exe).

"Jon Skeet [C# MVP]" <skeet@pobox.com> wrote in message
news:MPG.1d841dbe772edda098c661@msnews.microsoft.c om...[color=blue]
> Water Cooler v2 <wtr_clr@yahoo.com> wrote:[color=green]
>> I see this @ character a lot in front of C# strings. I searched the
>> documentation online but coult not come up with the page that explains
>> this symbol. What is it used for?[/color]
>
> See http://www.pobox.com/~skeet/csharp/s....html#literals
>
> --
> Jon Skeet - <skeet@pobox.com>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too[/color]


Bennie Haelen
Guest
 
Posts: n/a
#6: Nov 17 '05

re: The @ symbol in front of some strings


Using the "@" character in front of a string literal makes the string a
"Verbatim" Literal, instead of a "Regular" literal.

"Regular" literals need to use a backslash to escape, as for example in
a file path:

string filePath = "DirectoryA\\myfile.txt"

If you use the "@" character, you do not need to use escape characters,
as in:

string filePath = @"DirectoryA\myfile.txt"

HTH,

Bennie Haelen


Water Cooler v2 wrote:[color=blue]
> I see this @ character a lot in front of C# strings. I searched the
> documentation online but coult not come up with the page that explains
> this symbol. What is it used for?
>[/color]
Closed Thread