Once Upon a Time on Stack Overflow: The Curse of Long Strings in C#
Imagine this situation. You are writing a configuration JSON, SQL query, or, say, an HTML template directly in C# code. The string gets long, ugly, and completely unreadable. You press Enter to wrap the text... and get a compilation error. Sound familiar?
The question "Multiline string literal in C#" on Stack Overflow has gathered tens of thousands of views. And this is no coincidence. For a long time, C# was not the most friendly language for working with multiline text. But times are changing. In this article, we will journey from workarounds to the elegant solutions that appeared in C# 11.
If you write in C# and work with multiline string literals, this material will save you hours of Googling. Let's go!
The Problem: Why Simply Pressing Enter is a Bad Idea?
In C# (as in C++), a string literal is a sequence of characters enclosed in quotes. A line break inside such quotes is a syntax error. The compiler simply won't understand where the string ends.
Here is how NOT to do it:
// Compilation error!string query = "SELECT * FROM UsersWHERE Age > 18ORDER BY Name";The compiler will see "SELECT * FROM Users — and that's it. Everything after is chaos. In the past, developers managed as best they could. Let's break down the evolution of solutions.
The Old Way: Verbatim string (@-string) — A Lifesaver
The first thing that comes to mind is the verbatim string literal. You simply put @ before the opening quote. This disables the interpretation of escape sequences (except for double quotes "").
But there is a nuance: indentation also becomes part of the string.
Example with verbatim string:
string json = @"{ ""name"": ""Alice"", ""age"": 30}";Console.WriteLine(json);Output:
{ "name": "Alice", "age": 30}Pros:
- Works in all versions of C#.
- No need to escape backslashes (
\\).
Cons:
- Quotes inside must be doubled (
"") — this is terrible if you have a lot of JSON. - Left indentation (spaces/tabs) ends up in the final string.
- Cannot be directly mixed with string interpolation (
$) (but$@is possible).
This method is still popular, but it is not ideal for clean code. The real breakthrough came in C# 11.
The Newest Way: Raw string literal (C# 11 and .NET 7+)
Starting with C# 11, we have the raw string literal — literally a "raw string literal". It solves 99% of multiline problems.
Syntax: three or more double quotes in a row (""").
Example 1: Simple JSON without escaping
string json = """ { "name": "Alice", "age": 30 } """;Console.WriteLine(json);Output:
{ "name": "Alice", "age": 30}Magic: Quotes inside the string do not need to be doubled! The compiler understands that the string has ended only when it sees three consecutive quotes.
Example 2: SQL query with indentation
string query = """ SELECT u.Name, o.OrderDate FROM Users u JOIN Orders o ON u.Id = o.UserId WHERE o.Status = 'Active' ORDER BY o.OrderDate DESC """;Console.WriteLine(query);Important about indentation: The closing quotes (""") set the base indentation level. Any lines that have less indentation than the closing quotes will have that whitespace trimmed. This allows you to maintain code readability without cluttering the string with extra spaces.