String
literalsThese are for constant string
data in C# programs. String
data is created in different ways. We use literals as arguments to methods, or anywhere a string
is needed.
With a string
literal, characters are stored directly inside the metadata. Fewer indirections (which reduces performance) are needed.
Here the class
-level string
literals are represented as static
or const
references. The method-level ones are treated separately in the metadata.
string
literals are prefixed with the "at" symbol. This is the verbatim string
literal syntax.double
quotes.using System; class Program { static string _value1 = "String literal"; const string _value2 = "String literal 2"; const string _value3 = "String literal 3\r\nAnother line"; const string _value4 = @"String literal 4 Another line"; const string _value5 = "String literal\ttab"; const string _value6 = @"String literal\ttab"; static void Main() { // // Execution engine begins here. // string test1 = "String literal \"1\""; const string test2 = "String literal 2"; string test3 = @"String literal ""3"""; const string test4 = @"String literal 4"; // // Print out the string literals. // Console.WriteLine( "{0}\n{1}\n{2}\n{3}\n{4}\n{5}\n{6}\n{7}\n{8}\n{9}", _value1, _value2, _value3, _value4, _value5, _value6, test1, test2, test3, test4); } }String literal String literal 2 String literal 3 Another line String literal 4 Another line String literal tab String literal\ttab String literal "1" String literal 2 String literal "3" String literal 4
string
literalsIt is also possible to use 3 quotes to start (or end) a raw string
literal. This contains the inner characters exactly, but with left-side indentation removed.
using System; class Program { static void Main() { string text = """ Hello friend, How are you? """; Console.WriteLine(text); } }Hello friend, How are you?
Concat
Concatenating string
variables is done at runtime. But if a string
variable is constant, the compiler will generate intermediate language with the concatenations removed.
string
is used.using System; class Program { static void Main() { const string a = "Dot "; const string b = "Net "; const string c = "Perls"; Console.WriteLine(a + b + c); } }Dot Net Perls.method private hidebysig static void Main() cil managed { .entrypoint // Code size 11 (0xb) .maxstack 8 IL_0000: ldstr "Dot Net Perls" IL_0005: call void [mscorlib]System.Console::WriteLine(string) IL_000a: ret } // end of method Program::Main
Before the string
literals ever reach the metadata or the intermediate language instructions, the C# compiler applies an optimization called constant folding.
String
literal constants are separated and shared. Applying constant folding manually is not required for performance.string
literal in many places in a program, it is stored only once in the user strings stream.string
literals in C# programs.String
literals are specified with the string
verbatim syntax. We use the backslash to escape certain sequences. String
literals are constant—they cannot be changed.