What does '@' prefix on strings in C# do?

  • Thread starter Thread starter Grant Schenck
  • Start date Start date
Grant,

It means to treat the string as a literal. Without it, you can have
escape sequences in the string such as \t, \n and \r which represent single
characters (you use these because they are not printable characters). If
you use @, it treats the string exactly as it is.

Hope this helps.
 
Thanks. That makes sense.

I tried searching help but @ is a hard character to search on!

Grant Schenck

Nicholas Paldino said:
Grant,

It means to treat the string as a literal. Without it, you can have
escape sequences in the string such as \t, \n and \r which represent single
characters (you use these because they are not printable characters). If
you use @, it treats the string exactly as it is.

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Grant Schenck said:
I can't seem to find a documentation describing this.

Thanks!

Grant Schenck
 
It specifies that the following string is a literal, effectively ignoring
escape characters.

This can be quite useful when dealing with strings with file paths for
instance... instead of typing in a c++ way like:

string myString = “c:\\dir1\\dir2\\dir3\\file1.txtâ€;

You can do so in C# with the @ symbol as:

string myString = @“c:\dir1\dir2\dir3\file1.txtâ€;

Brendan
 
Nicholas said:
It means to treat the string as a literal. Without it, you can have
escape sequences in the string such as \t, \n and \r which represent single
characters (you use these because they are not printable characters). If
you use @, it treats the string exactly as it is.

To be even more exact where the terminology is concerned, the syntax
with the @ defines a so-called "verbatim string literal". A string
literal is every string in double quotes, but only the verbatim string
literal is treated as is and comes with the @.


Oliver Sturm
 
Back
Top