How do C# 11's raw string literals (`"""..."""`) simplify writing strings containing quotes, escape sequences, or multi-line content like JSON or SQL?
Short answer
Raw string literals (delimited by three or more double-quotes) let you write ANY content — including literal double quotes, backslashes, and multi-line text — WITHOUT any escaping at all, and support string interpolation via an adjusted number of leading '$' characters when…
Advertisement
AModel answer
Raw string literals (delimited by three or more double-quotes) let you write ANY content — including literal double quotes, backslashes, and multi-line text — WITHOUT any escaping at all, and support string interpolation via an adjusted number of leading '$' characters when needed, making embedded JSON, SQL, or regex patterns dramatically more readable.
string json = """
{
"name": "Sam",
"path": "C:\\Users\\Sam"
}
"""; // no escaping needed for quotes or backslashes at all
string interpolated = $"""
Hello, {name}! Your path is "C:\literal\path".
""";Real-world example
Embedding a readable, unescaped JSON payload, SQL query, or regex pattern directly as a C# string literal without escape-character clutter.
Likely follow-ups
- How does interpolation inside a raw string literal avoid ambiguity with literal '{' and '}' characters that might appear in embedded JSON?
Related topics