I have noticed this piece of code:
FileInfo[] files =new DirectoryInfo(@"C:\").GetFiles();
What is the purpose of @
? Are there other uses?
I have noticed this piece of code:
FileInfo[] files =new DirectoryInfo(@"C:\").GetFiles();
What is the purpose of @
? Are there other uses?
C# supports two forms of string literals: regular string literals and verbatim string literals.
A regular string literal consists of zero or more characters enclosed in double quotes, as in "hello", and may include both simple escape sequences (such as \t for the tab character) and hexadecimal and Unicode escape sequences.
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is @"hello". In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
Example 1:
@"C:\Path\File.zip" == "C:\\Path\\File.zip"
// where
"C:\\Path\\File.zip" // regular string literal
@"C:\Path\File.zip" // verbatim string literal
Note: In verbatim string literals you should escape double quotes.
Example 2:
@"He said: ""Hello""" == "He said: \"Hello\""
More info here:
The prefix "@" enables the use of keywords as identifiers, which is useful when interfacing with other programming languages. The character @ is not actually part of the identifier, so the identifier might be seen in other languages as a normal identifier, without the prefix. An identifier with an @ prefix is called a verbatim identifier. Use of the @ prefix for identifiers that are not keywords is permitted, but strongly discouraged as a matter of style.
Example:
class @class
{
public static void @static(bool @bool) {
if (@bool)
System.Console.WriteLine("true");
else
System.Console.WriteLine("false");
}
}
class Class1
{
static void M() {
cl\u0061ss.st\u0061tic(true);
}
}
The @ symbol basically says "take this string exactly as it is". It allows the developer to avoid escaping.
So, @"C:\" == "C:\\" (with the \ escaped).
I find it most useful in RegEx... RegEx can get really nasty and confusing quick when you start escaping different things, etc, so it's nice to just use the literal value.
the @ sign before a string allows for literal interpretation of the string, otherwise you'd have to escape the backslash with another ("C:\").
That's a verbatim string literal so you can use backslash without being interpreted as escape character.
A verbatim string literal consists of an
@
character followed by a double-quote character, zero or more characters, and a closing double-quote character. A simple example is@"hello"
. In a verbatim string literal, the characters between the delimiters are interpreted verbatim, the only exception being a quote-escape-sequence. In particular, simple escape sequences and hexadecimal and Unicode escape sequences are not processed in verbatim string literals. A verbatim string literal may span multiple lines.
quote-escape-sequence: ""
So, the only escaping the verbatim string literal does is escaping ""
as "
within two "s.
@ indicates that the string is a literal string and therefor you don't have to escape characters inside it. As the documentation for string puts it:
The advantage of verbatim strings is that escape sequences are not processed, which makes it easy to write, for example, a fully qualified file name: @"c:\Docs\Source\a.txt" rather than
"c:\\Docs\\Source\\a.txt"
Putting a @ before a string turns it from a regular literal string into a verbatim literal string that does not use the forward slash to escape () but uses two quotes in a row to escape a quote.
The @
symbol has a couple of meanings in C#:
When used at the beginning of the string, it means "take this string literally; do not interpret characters that would otherwise be used as escape characters." This is called the verbatim string literal. For example, @"\n\t"
is equal to "\\n\\t"
.
When used at the beginning of an identifier, it means "interpret this as an identifier, not as a keyword". For example, int @this
would allow you to name an integer variable "this", which would normally be illegal since this
is a keyword.