Raw string literal text - """ in string literals

A raw string literal starts and ends with a minimum of three double quote (") characters:

var singleLine = """This is a "raw string literal". It can contain characters like \, ' and ".""";

Raw string literals can span multiple lines:

var xml = """
        <element attr="content">
            <body>
            </body>
        </element>
        """;

The following rules govern the interpretation of a multi-line raw string literal:

  • Both opening and closing quote characters must be on their own line.
  • Any whitespace to the left of the closing quotes is removed from all lines of the raw string literal.
  • Whitespace following the opening quote on the same line is ignored.
  • Whitespace only lines following the opening quote are included in the string literal.

You may need to create a raw string literal that has three or more consecutive double-quote characters. Raw string literals can start and end with a sequence of at least three double-quote characters. When your string literal contains three consecutive double-quotes, you start and end the raw string literal with four double quote characters:

var moreQuotes = """" As you can see,"""Raw string literals""" can start and end with more than three double-quotes when needed."""";

If you need to start or end a raw string literal with quote characters, use the multi-line format:

var MultiLineQuotes = """"
               """Raw string literals""" can start and end with more than three double-quotes when needed.
               """";

Raw string literals can also be combined with interpolated strings to embed the { and } characters in the output string. You use multiple $ characters in an interpolated raw string literal to embed { and } characters in the output string without escaping them.

Raw string literals were introduced in C# 11.

See also