Multiline string literal in C#
Categories:
Mastering Multiline String Literals in C#

Explore various techniques for creating multiline string literals in C#, from traditional concatenation to modern verbatim strings and raw string literals, enhancing code readability and maintainability.
Handling multiline strings is a common requirement in programming, whether for embedding SQL queries, HTML templates, JSON data, or simply long blocks of text. C# offers several elegant ways to define string literals that span multiple lines, each with its own advantages and use cases. This article will guide you through the evolution of multiline string handling in C#, from older methods to the latest C# 11 features, helping you choose the best approach for your specific needs.
The Traditional Approach: Concatenation and Newline Characters
Before the introduction of more convenient syntax, developers relied on string concatenation and explicit newline characters (\n
or \r\n
) to create multiline strings. While functional, this method can quickly become cumbersome and reduce readability, especially for longer text blocks. It requires careful management of whitespace and escape sequences.
string multilineString = "This is the first line.\n" +
"This is the second line.\n" +
"And this is the third line.";
Console.WriteLine(multilineString);
Using concatenation and \n
for multiline strings
\n
insertion and makes the string content harder to visualize directly in the code.Verbatim String Literals: The @
Operator
Introduced early in C#, verbatim string literals, denoted by the @
symbol prefix, revolutionized multiline string handling. They allow strings to span multiple lines directly in the code, preserving all whitespace and treating backslashes (\
) as literal characters rather than escape sequences. This significantly improves readability for paths, regular expressions, and multiline text.
string verbatimString = @"This is the first line.
This is the second line.
With some leading spaces.
And this is the third line.";
Console.WriteLine(verbatimString);
// Example with file path
string filePath = @"C:\Program Files\MyApp\config.json";
Console.WriteLine(filePath);
Using verbatim string literals (@
) for multiline text and paths
""
). For example: @"He said ""Hello"""
.Interpolated Verbatim String Literals: Combining @
and $
C# 6 introduced string interpolation ($
), allowing expressions to be embedded directly within string literals. When combined with verbatim strings ($@
or @$
), you get the best of both worlds: multiline support, literal backslashes, and inline expression evaluation. This is a powerful and commonly used feature for constructing complex strings.
string name = "Alice";
int age = 30;
string interpolatedVerbatim = $@"Hello, {name}!
Your age is {age}.
This string also supports literal backslashes: C:\Users\{name}";
Console.WriteLine(interpolatedVerbatim);
Combining verbatim and interpolated strings for dynamic multiline content
flowchart TD A[Start] --> B{Need Multiline String?} B -->|Yes| C{Need Escape Sequences (e.g., \n)?} C -->|Yes| D[Use Concatenation + \n] C -->|No| E{Need Literal Backslashes or Interpolation?} E -->|Yes| F[Use Verbatim String (@"...")] E -->|No| G{Need Interpolation?} G -->|Yes| H[Use Interpolated Verbatim ($@"...")] G -->|No| I[Use Raw String Literal ("""...")] B -->|No| J[Use Single Line String] D --> K[End] F --> K H --> K I --> K J --> K
Decision flow for choosing the right multiline string approach in C#
Raw String Literals: The Modern Solution (C# 11+)
C# 11 introduced raw string literals, providing the most flexible and readable way to define multiline strings, especially those containing other languages like JSON, XML, or HTML. They start and end with at least three double-quotes ("""
) and automatically handle indentation and internal quotes without requiring escaping. This makes embedding complex text blocks incredibly clean.
string jsonContent = """
{
"name": "John Doe",
"age": 42,
"isStudent": false
}
""";
Console.WriteLine(jsonContent);
// Raw string literal with interpolation
string productName = "Laptop";
decimal price = 1200.50m;
string htmlTemplate = $$r"""
<div class="product-card">
<h1>{productName}</h1>
<p>Price: {price:C}</p>
</div>
""";
Console.WriteLine(htmlTemplate);
Using raw string literals for JSON and interpolated HTML
Choosing the Right Multiline String Type
With several options available, selecting the appropriate multiline string type depends on your specific needs:
- Concatenation (
+
with\n
): Best avoided for new code unless dealing with very short, dynamic strings built programmatically. - Verbatim String Literals (
@
): Excellent for file paths, regular expressions, or simple multiline text where backslashes should be literal and no interpolation is needed. - Interpolated Verbatim String Literals (
$@
or@$
): The go-to for multiline strings that require embedded expressions and literal backslashes. Highly versatile. - Raw String Literals (
"""
): The preferred choice for embedding large blocks of text, especially those in other formats like JSON, XML, or HTML, due to their superior readability and automatic indentation handling. They are also great when you need to include quotes without escaping.