Raw string literal in C# – (“””)

A raw string literal starts and ends with a minimum of three double quote (") characters. this raw string literals were introduced in C# 11.

Raw string literal in C#

A string literal is a sequence of characters enclosed in double quotes ("). To include a double quote character as part of the string, you can use an escape sequence, which is a backslash (\) followed by the character you want to include.

However, if you need to include multiple double quotes within a string. You can use the triple-quote (""") sequence to represent a single double-quote character.

For example, consider the following C# code:

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>
        """;

When working with multi-line raw string literals in C#, there are certain rules that dictate how these literals should be interpreted. These rules are as follows:

  1. The opening and closing quote characters must be on separate lines. This means that the opening quote must be on one line, and the closing quote must be on a different line. If these quotes are not on separate lines, the code will not compile.
  2. Any whitespace to the left of the closing quotes is removed from all lines of the raw string literal. This means that any whitespace to the left of the closing quote on each line will be removed from the string. For example, if there is extra whitespace to the left of the closing quotes on the last line of the string, this whitespace will be removed from all lines of the string.
  3. Whitespace following the opening quote on the same line is ignored. This means that any whitespace that follows the opening quote on the same line will be ignored and will not be included in the string. For example, if there is extra whitespace between the opening quote and the first character of the string on the same line, this whitespace will be ignored.
  4. Whitespace-only lines following the opening quote are included in the string literal. This means that any lines that consist only of whitespace (such as blank lines) that come after the opening quote will be included in the string. These whitespace-only lines will be part of the resulting string and will be preserved when the code is executed.

By following these rules, you can ensure that your multi-line raw string literals are interpreted correctly by the C# compiler. This will help you create cleaner and more readable code that is easier to understand and maintain over time.

it is sometimes necessary to create a raw string literal that contains three or more consecutive double-quote characters. To accomplish this, you can use a special syntax for starting and ending the raw string literal.

In general, raw string literals can start and end with a sequence of at least three double-quote characters. However, when your string literal contains three or more consecutive double-quotes, you need to use a slightly different syntax. To indicate that the raw string literal contains three or more consecutive double-quotes, you need to start and end the 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 strings 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.

You can also read “C# string interpolation using $

Why raw string literal is necessary C#?

The motivation behind the proposed feature is to provide a more convenient way to create string literals in C#. The current approaches require the manual escaping of special characters. Also, this does not allow for easy inclusion of other languages in the string literals. The use of fixed start/end delimiters in string literals also creates problems as the delimiter itself may be used within the string content.

The proposed feature addresses these issues by allowing for flexible start and end delimiters. That does not conflict with the content of the string. This will make it easier for developers to create and edit string literals in C#. Particularly for complex content such as regular expressions or multi-language documents.

Grate indentation examples

One way to understand the ‘indentation whitespace’ algorithm is to see how it operates on different input examples. Here are a few examples that illustrate how the algorithm works:

Example 1 – Standard case

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

is interpreted as

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

Example 2 – End delimiter on same line as content.

This is illegal. The last content line must end with a new_line.

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

Example 3 – End delimiter before start delimiter

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

is interpreted as

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

Example 4 – End delimiter after start delimiter

This is illegal. The lines of content must start with the ‘indentation whitespace

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

Example 5 – Empty blank line

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

          </element>
          """;

is interpreted as

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

Example 6 – Blank line with less whitespace than prefix (dots represent spaces)

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

is interpreted as

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

Example 7 – Blank line with more whitespace than prefix (dots represent spaces)

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

is interpreted as

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

Conclusion

String literals are sequences of characters that represent a string value. Developers can use escape sequences to include special characters within string literals. But working with multiple double quotes can be tedious.

To make things easier, C# provides verbatim string literals, which allow developers to include special characters without using escape sequences.

The triple-quote sequence (""") can be used to represent a single double-quote character within a string literal. This makes working with string literals that contain multiple double quotes much easier and more readable.

Leave a Comment