String literals in C# are vital for representing fixed character sequences directly within code, enhancing readability and clarity. They simplify string handling, support interpolation, and facilitate efficient memory usage through compiler optimizations like string interning. Thus, string literals are essential components that empower developers to work effectively with textual data.
Quoted String Literals
These literals are the most often used because they suit most straightforward cases.
The literals start and end with a single double-quote character (“).
var message = "Hello, World";
Console.WriteLine(message);
// Hello, World
We use them for single-line strings where no escaping is required.
But what if the string is more complex? Let’s say we have a piece of HTML.
<div class="greeting">
Hello, World
</div>
Then our quoted string literal gets messy.
var html =
"<div class=\"greeting\">\r\n\tHello, World\r\n</div>";
Console.WriteLine(html);
//<div class= "greeting">
// Hello, World
//</div >
It gets even worse if we want to reflect the lines in the code. We need to break the lines and use concatenation.
var html = "<div class=\"greeting\">" +
"\r\n\tHello, World\r\n" +
"</div>";
Verbatim String Literals
Verbatim string literals are a better choice for multi-line strings. To define a verbatim string literal, we put an at (@) sign at the beginning of the string.
var html = @"<div class=""greeting"">
Hello, World
</div>";
Console.WriteLine(html);
//<div class= "greeting">
// Hello, World
//</div >
It looks better without concatenation and escaping characters. Verbatim literals basically mean display it “as is”.
Only a quote escape sequence (“”) isn’t interpreted literally. It’s used to produce one double quotation mark.
The disadvantage of verbatim literal is indentations. You could notice the weird indentation in the literal but correct display in the console. That’s because the indentations are interpreted literally.
Using verbatim literals in the nested code can be a problem.
void Test()
{
if (true)
{
var html = @"<div class=""greeting"">
Hello, World
</div>";
Console.WriteLine(html);
//<div class="greeting">
// Hello, World
//</div >
}
}
It could look more pretty.
Raw String Literals
The raw string literals were introduced in C# 11.
They can contain whitespaces, new lines, embedded quotes, and other special characters without escape sequences.
The raw literal starts with at least three double quotes (“””) and ends with the same number of quotes. The starting and ending quotes must be in separate lines, and the content between them.
var html = """
<div class=""greeting"">
Hello, World
</div>
""";
Console.WriteLine(html);
//<div class= "greeting">
// Hello, World
//</div >
The raw literals also fix the issue with indentation, which verbatim literals have.
Why should raw literals start with at least three double quotes? Well, if you have three double quotes in your text, you should put four double quotes at the start and end of the literal, and so on.
var text = """"
The text with """ double quotes
"""";
Console.WriteLine(text);
// The text with """ double quotes
String Interpolation
String interpolation allows us to insert an expression into a string literal.
It’s easy with quoted string literals. You should put a dollar sign ($) before the literal and use curly braces for the expression.
var user = "Oleg";
var text = $"Hello, {user}";
Console.WriteLine(text);
// Hello, Oleg
Since C# 11, we can use multiline interpolated expressions. It can improve readability within curly braces.
var user = "Oleg";
var text = $"Hello, {
(user == "Oleg"
? "me"
: user)}";
Console.WriteLine(text);
// Hello, me
Also, we can use interpolation in the verbatim literals. We should put a dollar sign before or after at sign ($@), (@$). The order doesn’t matter.
var user = "Oleg";
var html = $@"
<div class=""greeting"">
Hello, {user}
</div>";
Console.WriteLine(html);
//<div class= "greeting">
// Hello, Oleg
//</div >
The most interesting is interpolation in the raw string literals. For basic, where you don’t have curly braces in the text, it’s the same as for other literals. You should put a dollar sign ($) before triple double quotes.
var user = "Oleg";
var html = $"""
<div class=""greeting"">
Hello, {user}
</div>
""";
Console.WriteLine(html);
//<div class= "greeting">
// Hello, Oleg
//</div >
Let’s say we want to set up JSON for our unit tests. JSON contains curly braces, so we need to use double curly braces for interpolation.
To escape JSON’s curly braces, we need to put two dollar signs, which means we need to use double curly braces for interpolation.
var user = "Oleg";
var html = $$"""
{
"Name": "{{user}}"
}
""";
Console.WriteLine(html);
//{
// "Name": "Oleg"
//}
If you have double curly braces in your text, you should put three dollar signs at the literal start and use triple curly braces for interpolation.