• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 260
  • Last Modified:

Why use @ when declaring strings

I have come across some code that uses @ when declaring a string.  I'm curious, can anyone tell me why you would want to do this?  For example, I've seen:

string finalPath = @"\\agentdev\agyroot\common\tempUpload\NCQ\";

Why not just do:

string finalPath = "\\agentdev\agyroot\common\tempUpload\NCQ\";

0
russomr
Asked:
russomr
2 Solutions
 
Guy Hengel [angelIII / a3]Billing EngineerCommented:
with @ the \ are not considered to be escape characters
0
 
svyCommented:
@-quoting  allows for the escape sequences not to be processed,
these are escape sequences:
\' Single quote
\" Double quote
\\ Backslash
\nnn Octal number (nnn)
\0 Null character (really just the octal number zero)
\a Audible bell
\b Backspace
\f Formfeed
\n Newline
\r Carriage return
\t Horizontal tab
\v Vertical tab
\xnnn Hexadecimal number (nnn)

So in order to have this string "\\agentdev\agyroot\common\tempUpload\NCQ\"
one has two options:
"\\\\agentdev\\agyroot\\common\\tempUpload\\NCQ\\"
or
@"\\agentdev\agyroot\common\tempUpload\NCQ\"
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now