Skip to content

Raw string literals C# 11.0code reductionreadability

Allow strings containing newlines and quotes without escaping.

C# 11.0 introduces raw string literals which allow you to write strings containing newlines and quotes without escaping them.

Interpolated strings can also be prefixed with $""" to indicate that the string is an interpolated raw string literal. Adding additional $ prefix characters increases the number of { and } characters required to indicate an interpolated expression allowing for unescaped { and } characters in the string.

Code

C#
string friend = """
    Hello "Friend"
    How are you?
    """;

string personal = $"""
    Hello {name}!
    How are you?
    """;

string templated = $$"""
    Use { Hello + {{varName}} }
    """;
C#
string friend = "Hello \"Friend\"\n\tHow are you?";

string personal = $"Hello {name}!\n\tHow are you?";

string templated = $"Use {{ Hello + {varName} }}";

Notes

  • You can use any number of " characters to delimit the string, so long as you have at least three
  • For multi-line raw strings, the opening """ must be followed by a newline and the closing """ must be on its own line
  • The indentation of the closing """ determines how much leading whitespace is stripped from each line

More information