Moon
Moon

Reputation: 22565

when to use @ in c#?

I use @ symbol with local path only, but when do I use @ exactly?

Upvotes: 20

Views: 13461

Answers (5)

ChrisF
ChrisF

Reputation: 137148

You use @ before strings to avoid having to escape special characters.

This from the MSDN:

Use verbatim strings for convenience and better readability when the string text contains backslash characters, for example in file paths. Because verbatim strings preserve new line characters as part of the string text, they can be used to initialize multiline strings. Use double quotation marks to embed a quotation mark inside a verbatim string. The following example shows some common uses for verbatim strings:

string filePath = @"C:\Users\scoleridge\Documents\"; //Output: C:\Users\scoleridge\Documents\

string text = @"My pensive SARA ! thy soft cheek reclined
    Thus on mine arm, most soothing sweet it is
    To sit beside our Cot,..."; /* Output: My pensive SARA ! thy soft cheek reclined    Thus on mine arm, most soothing sweet it is    To sit beside our Cot,...
*/

string quote = @"Her name was ""Sara."""; //Output: Her name was "Sara."

Upvotes: 42

Sauron
Sauron

Reputation: 16903

If you want to use keywords as variable names

string @string = "Hi";

Upvotes: 5

Chris Morley
Chris Morley

Reputation: 2426

You can prefix a string with the @ sign to avoid having to type 2 backslashes to mean one backslash. This is why it is often used for local path because it saves some typing, and simplifies what you are looking at when you read it later. When you have a bunch of double quotes and other escape characters, aka special characters - that's when you want the @ sign. When you use the @ sign, make sure to put just one backslash when you mean backslash. When using the @ you want to use the double quote character, put two double quotes instead of backslash, double quote.

String path = "C:\\path\\to\\my\\file";

vs

String path = @"C:\path\to\my\file"; //@ says one backslash is enough

here is another example:

String quotation = "He said, \"Wow!\""; //backslashes say not to end string

vs.

String quotation = @"He said, ""Wow!"""; //need a different method of denoting double quote

Upvotes: 3

Aamir
Aamir

Reputation: 15566

AFAIK, You can use @ at any place where you don't want to let the default meaning of the thing persist. For example, @class will make class an identifier. @bool will make bool an identifier instead of a keyword.

You know the usage of @ before strings. It is used to indicate that take all the text of the string literally and don't treat any character in the string specially.

Edit: An yes, another thing to this is that @Keyword is compiled into a Keyword in IL.

See this Link for more details.

Upvotes: 31

Rob
Rob

Reputation: 48369

Verbatim Strings

An @ before a string literal in C# denotes a verbatim string. In a verbatim string, only the quote escape sequence ("") is parsed as an escape sequence; all others, e.g. \n, \t etc. are ignored.

You'll have seen this syntax used with file paths, etc. because it's convenient to have the compiler ignore the backslashes in the path, rather than having to double-escape them, e.g.

var s = @"c:\Some\File\Path.txt";

is a little easier to read than

var s = "c:\\Some\\File\\Path.txt";

Reserved Words

You can also prefix identifiers with @ in order to allow using reserved words in identifiers. For example, @class can be used as an identifier, whereas class wouldn't be permitted. In this specific case, @class is a little less jarring (at least, I find) than the usual convention of klass or clazz which are often used to work around this restriction in other languages.

Upvotes: 25

Related Questions