Skip to content
Tutorials Center
My learning

Topics

C# Reference ยท Types and Literals

Raw string literals

Reference for C# raw string literals: triple-quote syntax, multi-line indentation rules, quotes and braces inside, interpolation with $$ and common errors.

A raw string literal, added in C# 11, starts and ends with three or more double quotes. Everything between the delimiters is taken exactly as written: no escape sequences, no doubled quotes, no backslash processing. That makes it the cleanest way to embed JSON, XML, regular expressions, SQL, Windows paths and code samples.

For multi-line raw strings, the closing delimiter sits on its own line, and its indentation is removed from every content line, so you can indent the text to match your code without the spaces ending up in the string.

C#
string json = """
    {
      "name": "Ada"
    }
    """;

Try it

C# Single-line and multi-line raw strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

C# Interpolated raw strings

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with Console.ReadLine()), it gets the next line from this box.

Syntax

C#
"""text"""                 // single line
"""
    content lines
    """                    // multi-line: delimiter on its own line
$$"""{{expression}}"""     // interpolated; N dollars => holes use N braces
"""u8                      // UTF-8 bytes (see the u8 page)
Rule Detail
Delimiter Three or more ". Opening and closing must have the same count. Use more quotes than the longest run of quotes inside the content.
Multi-line The opening """ must be followed by a line break, and the closing """ must be on its own line. The first line break and the last line break are not part of the string.
Indentation Whitespace before the closing delimiter is removed from every line. Every non-blank line must start with exactly that whitespace.
Interpolation $ count sets the brace count for holes. A run of fewer braces than the count is literal text. A run of the count or more braces that you want literally needs a bigger $ count.

Errors. The compiler rejects: content on the same line as a multi-line opening delimiter, content on the closing delimiter's line, a content line indented less than the closing delimiter. Mixing tabs and spaces in the stripped indentation also fails.

Usage notes

  • No escapes at all. """\n""" is a backslash followed by n, not a newline. If you need a real escape, use an ordinary or interpolated expression and concatenate.
  • Newlines. The line breaks inside the literal are whatever your source file contains (CRLF or LF), so tests that compare text should not assume one form.
  • Use for regex and JSON. Raw strings avoid the doubled backslashes of normal literals and the doubled quotes of verbatim ones.
  • Inside interpolations you can use any expression, including calls with string arguments, format specifiers and alignment ({value,8:F2}).
  • Constants. A raw string literal without interpolation can initialize a const string, and from C# 10 so can an interpolated string whose holes are all constants.
  • Blank lines inside the literal keep no indentation requirement.
  • Version. Requires C# 11 (.NET 7 or later compilers).