Raw String Literals

C# 11.0 .NET 7.0

Published Updated Author Jeffrey T. Fritz Reading time

Raw string literals are delimited by three or more quotes and eliminate escape-sequence processing, making it easy to work with JSON, SQL, and multiline text.

Introduced in C# 11.0, raw string literals use a new syntax with three (or more) double-quotes (""") to denote string boundaries. Within a raw string, all characters are treated literally—no escape sequences are processed. This is ideal for embedding JSON, SQL queries, XML, and other content that contains many special characters that would normally require escaping.

Why it matters

Raw string literals dramatically improve readability when working with structured text. JSON, SQL, regular expressions, and multiline content can now be written naturally without escaping quotes or backslashes. When combined with string interpolation, they enable clean embedded queries and templates, making code far more maintainable and easier to understand at a glance.

Cautions

Raw strings cannot contain the closing delimiter (three quotes) as a substring unless you use more quotes in the delimiter (e.g., """" for four quotes). Additionally, raw string indentation is significant—the actual content starts on the first line after the opening delimiter, and leading whitespace is preserved. Be careful with multiline formatting in your source code, as it affects the actual string content.

Basic raw string literals

Raw strings eliminate escape sequences, allowing you to write JSON, SQL, and multiline text naturally without escaping quotes or backslashes.

Valid since C# 11.0

// JSON without raw strings - requires escaping quotes
string jsonOld = "{\"name\": \"Alice\", \"age\": 30}";

// JSON with raw strings - clean and readable
string jsonNew = """
{
    "name": "Alice",
    "age": 30,
    "email": "alice@example.com"
}
""";

// SQL query with raw strings
string sqlQuery = """
SELECT u.Id, u.Name, u.Email
FROM Users u
WHERE u.Status = 'Active'
ORDER BY u.Name
""";

// Regex pattern with raw strings
string patternOld = "^\\w+@[\\w\\.]+\\.\\w+$";
string patternNew = """^\\w+@[\w\.]+\.\w+$""";

Console.WriteLine(jsonNew);
Console.WriteLine(sqlQuery);

Raw strings with interpolation

Combine raw string literals with interpolation for clean, readable embedded queries and templates with variable substitution.

Valid since C# 11.0

// Raw string with interpolation
string name = "Alice";
int age = 30;
string email = "alice@example.com";

// Combine raw strings with interpolation ($)
string jsonResponse = $$"""
{
    "name": "{{name}}",
    "age": {{age}},
    "email": "{{email}}"
}
""";

// Dynamic SQL query with safe parameter substitution
string userId = "42";
string sqlQuery = $$"""
SELECT * FROM Orders
WHERE UserId = {{userId}}
AND OrderDate >= DATEADD(day, -30, GETDATE())
""";

Console.WriteLine(jsonResponse);
Console.WriteLine(sqlQuery);

Learn more

Raw String Literals (Microsoft Learn)