C# 11 – Interpolated raw string literal
C# 11 introduces the concept of raw string literals to the language and with that come a set of new features for string interpolation as well.
First of all, you can continue to use the interpolation syntax as you know it in combination with raw string literals like this:
var x = 5, y = 4;
var interpolatedRaw = $"""The sum of "{x}" and "{y}" is "{ x + y }".""";
Code language: C# (cs)
The output will be:
The sum of "5" and "4" is "9".
Code language: plaintext (plaintext)
Escaping braces { and }
You can escape braces by doubling them. If we take the example above and double the braces:
var interpolatedRaw= $"""The sum of "{{x}}" and "{{y}}" is "{{ x + y }}".""";
Code language: C# (cs)
The output will be:
The sum of "{x}" and "{y}" is "{ x + y }".
Code language: plaintext (plaintext)
As you can see, the braces no longer play an interpolation role, and each double brace ends up as a single brace in the output.
Multiple $ characters in interpolated raw string literals
You can use multiple $ characters in an interpolated raw string literal in a similar manner to the “”” sequence. The number of $ characters that you use at the start of the string determines the number of { and } you need for string interpolation.
For example, the two strings below will output the exact same thing as our initial example:
var interpolatedRaw2 = $$"""The sum of "{{x}}" and "{{y}}" is "{{ x + y }}".""";
var interpolatedRaw3 = $$$"""The sum of "{{{x}}}" and "{{{y}}}" is "{{{ x + y }}}".""";
Code language: C# (cs)
Conditional operator in interpolated string
The colon (:) has special meaning in interpolated strings, and as a result, conditional expressions need an additional set of round brackets ( ) to work. For example:
var conditionalInterpolated = $"I am "{x}" year{(age == 1 ? "" : "s")} old.";
Code language: C# (cs)
Errors
Error CS9006 The interpolated raw string literal does not start with enough ‘$’ characters to allow this many consecutive opening braces as content.
This compiler error occurs when your string contains a sequence of brace characters which is equal to or greater than double the lenght of the sequence of the $ characters found at the start of your string.