How to Use Raw String Literals in C
Raw string literals, introduced in C# 11, provide a cleaner way to work with multiline text, JSON payloads, HTML, SQL queries, and strings containing special characters without relying on complex escape sequences. This guide covers how raw string literals work, their syntax rules for single-line and multiline formatting, and how to combine them with string interpolation.
Basic Syntax
A raw string literal starts and ends with at least three double-quote
characters ("""). Inside the literal, characters are
interpreted verbatim without escape sequences like \",
\n, or \\.
// Standard raw string literal
string singleLine = """This is a "quoted" string without escape characters.""";
Console.WriteLine(singleLine);
// Output: This is a "quoted" string without escape characters.If your text needs to contain three or more consecutive double-quote characters, you can open and close the string with one more quote than the sequence contained within the content:
// Using four quotes to allow three quotes inside the string
string quotesInside = """"This string contains """three consecutive quotes""" without escaping."""";Multiline Strings and Indentation
For multiline raw string literals, the opening and closing delimiters
must be on their own separate lines. Any text on the line following the
opening """ starts the string content, and the indentation
of the closing delimiter determines the left margin of the entire
string.
string json = """
{
"name": "Jane Doe",
"role": "Developer"
}
""";
Console.WriteLine(json);How Indentation Works
- Any whitespace to the left of the closing
"""is considered base indentation and is removed from every preceding line in the output. - Every line of content must have at least as much whitespace indentation as the closing delimiter, or the compiler will throw an error.
- Trailing newline characters match the line endings of the source file unless normalized.
String Interpolation with Raw Literals
You can combine raw string literals with string interpolation by
prefixing the opening quotes with one or more dollar signs
($).
The number of $ symbols determines how many consecutive
curly braces are required to trigger an interpolation expression. Any
fewer consecutive braces are treated as literal text. This is
particularly useful for formats like JSON or regex that naturally use
curly braces.
string name = "Alice";
int age = 30;
// Two '$' mean that '{{' and '}}' are required to interpolate values.
// Single '{' and '}' are treated as literal characters.
string jsonWithInterpolation = $$"""
{
"user": "{{name}}",
"age": {{age}},
"metadata": { "active": true }
}
""";
Console.WriteLine(jsonWithInterpolation);Key Rules to Remember
- Delimiters: Use at least three
"characters. The closing delimiter must match the number of opening delimiters. - Line Placement: In multiline strings, the opening
"""must be followed by a newline, and the closing"""must be on its own line. - Indentation Control: The position of the closing quotes sets the baseline whitespace strip for all lines above it.
- Interpolation Counting: The count of
$symbols matches the number of braces required for an interpolation expression.