ECMA-334 C# Language Specification9.4.4.5: String literals |
A verbatim string literal consists of an @ character followed by a double-quote character, zero or more
characters, and a closing double-quote character. 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.
regular-string-literal verbatim-string-literal " regular-string-literal-charactersopt " regular-string-literal-character regular-string-literal-characters regular-string-literal-character single-regular-string-literal-character simple-escape-sequence hexadecimal-escape-sequence unicode-escape-sequence new-line-character @" verbatim-string-literal-charactersopt " verbatim-string-literal-character verbatim-string-literal-characters verbatim-string-literal-character single-verbatim-string-literal-character quote-escape-sequence "" regular-string-literal-character must be one of the following characters: ', ", \, 0, a, b, f, n, r, t, u, U, x, v. Otherwise, a compile-time error occurs. end note]
shows a variety of string literals. The last string literal, j, is a verbatim string literal that spans multiple lines. The characters between the quotation marks, including white space such as new line characters, are preserved verbatim. end example]
string a = "Happy birthday, Joel"; // Happy birthday, Joel
string b = @"Happy birthday, Joel"; // Happy birthday, Joel
string c = "hello \t world"; // hello world
string d = @"hello \t world"; // hello \t world
string e = "Joe said \"Hello\" to me"; // Joe said "Hello" to me
string f = @"Joe said ""Hello"" to me"; // Joe said "Hello" to me
string g = "\\\\server\\share\\file.txt"; // \\server\share\file.txt
string h = @"\\server\share\file.txt"; // \\server\share\file.txt
string i = "one\r\ntwo\r\nthree";
string j = @"one
two
three";
The type of a string-literal is string.
Each string literal does not necessarily result in a new string instance. When two or more string literals that are equivalent according to the string equality operator (14.9.7), appear in the same assembly, these string literals refer to the same string instance.
is True because the two literals refer to the same string instance. end example]
class Test
{
static void Main() {
object a = "hello";
object b = "hello";
System.Console.WriteLine(a == b);
}
}