C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
String data is created in different ways. We use literals as arguments to methods, or anywhere a string is needed. They are stored directly inside the metadata.
Example. This program contains string literals at both the class level and the method level. The class-level string literals are represented as static or const references. The method-level ones are treated separately in the metadata.
Next: The program demonstrates literals. It has newlines, tabs and quotes in the literals.
Newlines: These are specified with either "\r\n" or just "\n." And tabs are specified with "\t."
Quotes: For quotes, we often use "\"", but for a verbatim literal (prefixed with @), we use two quotes to mean a quote.
Based on: .NET 4.5 C# program that uses string literals 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); } } Output String literal String literal 2 String literal 3 Another line String literal 4 Another line String literal tab String literal\ttab (The tab escape sequence was not recognized) String literal "1" (Uses quote escaping \") String literal 2 String literal "3" (Uses double quote escaping "") String literal 4
The program defines six class-level strings on the Program class type. The first string literal reference is a static variable, which means it will be referenced in the intermediate language in method bodies where it is used.
Note: Using the static string will require the execution engine to resolve the _value1 token in the metadata.
However: You can reassign the _value1 variable anywhere in your program where it is accessible, such as in the Main method.
At @ symbol on strings. Four of the string literals shown in the program are prefixed with the @ symbol before the quotation marks. This symbol indicates you are using the verbatim string literal syntax.
Tip: You can see that the backslash is treated as a character and not an escape sequence when the @ is used.
Also: The C# compiler allows you to use real newlines in verbatim literals. You must encode quotation marks with double quotes.
The program writes to the screen the contents of the string literals declared throughout the Program class. Note how the quotation marks are displayed in the test1 and test3 variables. The double quotes are printed as quotes.
Concatenating string variables in C# programs is done at runtime. On the other hand, if a string variable is constant and is known at compile-time, the compiler will generate intermediate language with the concatenations removed.
Next: This program appears to concatenate three strings. When compiled the IL shows that only one string is used.
C# program that concats string literals using System; class Program { static void Main() { const string a = "Dot "; const string b = "Net "; const string c = "Perls"; Console.WriteLine(a + b + c); } } Output Dot Net Perls Intermediate language: IL .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 that underlies all executable C# programs. The metadata format defined in the Common Language Specification 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 used in the entire program's strings.
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. String literal constants are separated and shared.
If you use a certain string literal in many places in your program, it is stored only once in the user strings stream in the metadata. Applying constant folding manually is not required for performance.
Thus: We applied the compiler technique of constant folding to string literals stored in high-level C# source programs.
Summary. String literals can be specified with the string verbatim syntax. We use the backslash to escape certain sequences (such as quote marks). String literals apply many optimizations. They are fast but cannot be changed.