C# raw string literals
last modified November 15, 2026
This article shows how to use raw string literals in C#. We cover single-line and multi-line raw strings, indentation rules, embedded quotes, and raw interpolated strings.
A raw string literal, introduced in C# 11, starts and ends with at
least three double quote characters ("""). Everything between
the delimiters is taken literally: backslashes are not escape characters and
single or double quotes need no escaping. Raw strings are designed for
embedding text in other formats, such as JSON, XML, HTML, SQL, regular
expressions, or source code.
Compared to verbatim strings (@"..."), raw strings do
not require doubling of quotes, and multi-line raw strings remove the
indentation that comes from the surrounding code.
Single-line raw strings
A single-line raw string has its content on the same line as the delimiters.
string path1 = "C:\\Users\\Jan\\Documents";
string path2 = @"C:\Users\Jan\Documents";
string path3 = """C:\Users\Jan\Documents""";
Console.WriteLine(path1);
Console.WriteLine(path2);
Console.WriteLine(path3);
string quote1 = "She said \"hello\" to me.";
string quote2 = @"She said ""hello"" to me.";
string quote3 = """She said "hello" to me.""";
Console.WriteLine(quote1);
Console.WriteLine(quote2);
Console.WriteLine(quote3);
string pattern = """^\d{3}-\d{4}$""";
Console.WriteLine(pattern);
We compare regular, verbatim, and raw string literals for text with backslashes and quotes.
string path1 = "C:\\Users\\Jan\\Documents"; string path2 = @"C:\Users\Jan\Documents"; string path3 = """C:\Users\Jan\Documents""";
In a regular string, each backslash must be escaped. Verbatim and raw strings take the backslashes as they are.
string quote1 = "She said \"hello\" to me."; string quote2 = @"She said ""hello"" to me."; string quote3 = """She said "hello" to me.""";
A regular string escapes a quote with a backslash, a verbatim string doubles it. In a raw string, the quote is written as it is.
string pattern = """^\d{3}-\d{4}$""";
Regular expressions are full of backslashes, which makes them a typical use case for raw strings.
$ dotnet run
C:\Users\Jan\Documents
C:\Users\Jan\Documents
C:\Users\Jan\Documents
She said "hello" to me.
She said "hello" to me.
She said "hello" to me.
^\d{3}-\d{4}$
Multi-line raw strings
In a multi-line raw string, the opening delimiter must be the last thing on its line and the closing delimiter must be on its own line. The newlines after the opening delimiter and before the closing delimiter are not part of the string. The whitespace in front of the closing delimiter determines how much indentation is removed from every line.
string poem = """
The old pond
a frog jumps in
the sound of water
""";
Console.WriteLine($"[{poem}]");
string indented = """
first line
second line
""";
Console.WriteLine($"[{indented}]");
string html = """
<ul>
<li>sky</li>
</ul>
""";
Console.WriteLine($"[{html}]");
We print several multi-line raw strings in brackets to show where they begin and end.
string poem = """ The old pond a frog jumps in the sound of water """;
The closing delimiter is indented by four spaces, so four spaces are removed from each line. The relative indentation of the lines is kept. The string does not start or end with a newline.
string indented = """ first line second line """;
Here the closing delimiter is indented less than the content, so part of the indentation stays in the string. We can control the indentation of the result with the position of the closing delimiter.
string html = """ <ul> """;
To start or end the string with a newline, we add an empty line after the opening delimiter or before the closing one.
$ dotnet run
[The old pond
a frog jumps in
the sound of water]
[ first line
second line]
[
<ul>
<li>sky</li>
</ul>
]
Quotes in raw strings
A raw string can contain any sequence of quotes shorter than its delimiter. When the content has three consecutive quotes, we use a delimiter of four quotes, and so on. There is no upper limit.
string code = """"
string s = """raw text""";
"""";
Console.WriteLine(code);
string doc = """""
Text with """ and """" inside.
""""";
Console.WriteLine(doc);
We embed raw string literals in raw string literals.
string code = """" string s = """raw text"""; """";
The string contains a C# raw string with three quotes, so the outer literal is delimited with four.
string doc = """"" Text with """ and """" inside. """"";
With a five-quote delimiter, the content may contain runs of three and four quotes.
$ dotnet run string s = """raw text"""; Text with """ and """" inside.
Raw interpolated strings
Raw strings can be combined with string interpolation by putting a
$ before the opening delimiter. The number of $
signs sets how many braces open and close an interpolation. Fewer
consecutive braces are taken literally.
string name = "Lucy";
int age = 28;
string text = $"""
Name: "{name}"
Age: {age}
Next: {age + 1}
""";
Console.WriteLine(text);
string json = $$"""
{
"name": "{{name}}",
"age": {{age}},
"tags": { "admin": {{(age > 25).ToString().ToLower()}} }
}
""";
Console.WriteLine(json);
string css = $$$"""
.user::before { content: "{{{name}}} ({{age}})"; }
""";
Console.WriteLine(css);
We build plain text, JSON, and CSS with raw interpolated strings.
string text = $"""
Name: "{name}"
""";
With a single $, interpolations use single braces, like in a
regular interpolated string. The quotes around the name need no escaping.
string json = $$"""
"name": "{{name}}",
"tags": { "admin": {{(age > 25).ToString().ToLower()}} }
""";
JSON uses braces for objects. With $$, an interpolation needs
two braces, and the single braces of the JSON objects are literal text.
Building JSON this way is fine for templates and tests; for real data,
use a serializer, which handles escaping.
string css = $$$"""
.user::before { content: "{{{name}}} ({{age}})"; }
""";
With $$$, three braces are needed. The {{age}}
part has only two, so it is printed as literal text.
$ dotnet run
Name: "Lucy"
Age: 28
Next: 29
{
"name": "Lucy",
"age": 28,
"tags": { "admin": true }
}
.user::before { content: "Lucy ({{age}})"; }
Practical use
Raw strings keep embedded documents readable. We can copy a JSON document, a regular expression, or a query from another tool and paste it into the code without editing it.
using System.Text.Json;
using System.Text.RegularExpressions;
var doc = JsonDocument.Parse("""
{
"city": "Bratislava",
"population": 475503,
"rivers": ["Danube", "Morava"]
}
""");
var root = doc.RootElement;
Console.WriteLine(root.GetProperty("city").GetString());
Console.WriteLine(root.GetProperty("rivers")[0].GetString());
var rx = new Regex("""
(?<user>[\w.]+) # user name
@
(?<host>[\w.]+) # domain
""", RegexOptions.IgnorePatternWhitespace);
var m = rx.Match("Contact: jan.novak@example.com");
Console.WriteLine($"{m.Groups["user"].Value} at {m.Groups["host"].Value}");
string sql = """
SELECT name, price
FROM cars
WHERE price > @price
ORDER BY price DESC
""";
Console.WriteLine(sql);
We parse JSON, match a commented regular expression, and print an SQL query.
var doc = JsonDocument.Parse("""
""");
A raw string can be passed directly as a method argument.
(?<user>[\w.]+) # user name @ (?<host>[\w.]+) # domain """, RegexOptions.IgnorePatternWhitespace);
With the IgnorePatternWhitespace option, the regular expression
engine ignores whitespace and allows comments after #. A
multi-line raw string lets us lay out a complex pattern over several lines.
$ dotnet run Bratislava Danube jan.novak at example.com SELECT name, price FROM cars WHERE price > @price ORDER BY price DESC
Indentation errors
Every content line of a multi-line raw string must start with the same whitespace as the closing delimiter. Otherwise, the compiler cannot decide what to remove and reports an error.
string bad = """
first line
second line
""";
Console.WriteLine(bad);
The second content line is indented less than the closing delimiter.
second line """;
The code does not compile; the error points to line 3. Empty lines are exempt from this rule. Tabs and spaces are not interchangeable: the whitespace must match character by character, so mixing tabs and spaces also triggers this error.
$ dotnet build Program.cs(3,1): error CS8999: Line does not start with the same whitespace as the closing line of the raw string literal.
Source
In this article we have worked with raw string literals in C#. See also the string, string interpolation, regular expressions, and JSON tutorials.
Author
List all C# tutorials.