
String data is created in different ways. Constant string data can be specified directly. The C# language provides string literals for this purpose. String literals are used as arguments, for display, and for storage on the disk. Two syntax forms are available: the classic C-like syntax and a verbatim syntax.
This C# tutorial demonstrates string literals. String literals are enclosed in quotes.

Let's look at a program that 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 string literals are treated separately in the metadata but use the same lexical syntax as the class-level literals. The program demonstrates newlines, tabs, and quotation marks in the literals.
Program that uses string literals [C#]
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
Overview. 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. 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.
Static String"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. This alters the encoding and interpretation of the string data.

Tip: You can see that the backslash is treated as a character and not an escape sequence when the @ is used. The C# compiler also allows you to use real newlines in verbatim literals. You must encode quotation marks with double quotes.
Output. 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 in the verbatim string literal are printed as (") quotes. The backslash \ escape character works correctly only in the string literals that don't use the @ verbatim syntax.
Console.WriteLineConcatenating 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 C# compile will generate intermediate language with the concatenations removed. This program appears to concatenate three strings, but when compiled the intermediate language shows that only one string is used.
String Concat ProgramsProgram that concats string literals [C#]
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
.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
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. There are several predefined streams in the metadata files, including the #Strings stream and the #US (user strings) stream.
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 simply a concatenated series of characters used in the entire program's strings. The execution engine stores the offsets and tables in memory and then simply reads a range in the #US stream when you use the string literal.
Note: We looked at the metadata format itself to get an idea of how the Common Language Infrastructure deals with string literals. It places them in a single stream.
Constant folding. Before the string literals ever reach the metadata or the intermediate language instructions, the C# compiler applies an optimization called constant folding where the constants in the program such as string literals are separated and shared.

Performance. So 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 should be done to improve readability of the high-level source code, not for performance.
Compiler Explanation Expert .NET 2.0 IL Assembler ReviewNote: We applied the compiler technique of constant folding to string literals stored in high-level C# source programs.

We saw how you can use string literals in your C# programs with the string verbatim literal syntax and with the backslash to escape certain sequences. You must escape quote marks in the string literals. String literals apply many optimizations and yield improved performance over other strings.
String Type