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
  • Newlines are now supported in all string interpolation expressions in C# 11
  • Type parameters are limited to the same subset that typeof supports - namely you can no use dynamic, nullable reference types like string? or tuple types like (int, int)

More information