Home
Search
String LiteralUse string literals, which are enclosed in quotes. A string literal is constant.
C#
This page was last reviewed on Aug 9, 2021.
String literal. This is constant string data. 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.
Example program. Here the class-level string literals are represented as static or const references. The method-level ones are treated separately in the metadata.
Newlines These are specified with either "\r\n" or just "\n." And tabs are specified with "\t."
Quotes For quotes, we often use a backslash, but for a verbatim literal, we use two quotes to mean a quote.
At symbol Four of the string literals are prefixed with the "at" symbol. This is the verbatim string literal syntax.
Tip The C# compiler allows you to use real newlines in verbatim literals. You must encode quotation marks with 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
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.
Next This program appears to concatenate 3 strings. When compiled the IL shows that only one string is used.
string.Concat
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
Metadata. String literals are stored in the metadata format. This is defined in the Common Language Specification. It includes a database of tables with headers and rows in all exe files.
Tip There are several predefined streams in the metadata files, including the #Strings stream and the #US (user strings) stream.
The #US stream is used to store programmer-defined literals. The metadata tables store offsets into this stream. The stream itself is a concatenated series of characters.
Note The execution engine stores the offsets and tables in memory and then reads a range in the #US stream.
Performance. Before the string literals ever reach the metadata or the intermediate language instructions, the C# compiler applies an optimization called constant folding.
Here String literal constants are separated and shared. Applying constant folding manually is not required for performance.
Storage If you use a certain string literal in many places in a program, it is stored only once in the user strings stream.
Thus We see the compiler technique of constant folding applied to string literals in C# programs.
A summary. 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Aug 9, 2021 (edit).
Home
© 2007-2022 sam allen.
see site info on the changelog.