Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Raw string literal text -
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:
- The opening quotes must be the last non-whitespace characters on their line, and the closing quotes must be the first non-whitespace characters on their line.
- Any whitespace to the left of the closing quotes is removed from all lines of the raw string literal.
- Any whitespace following the opening quotes on the same line is ignored.
- Whitespace only lines following the opening quote are included in the string literal.
- If a whitespace precedes the end delimiter on the same line, the exact number and kind of whitespace characters (for example, spaces vs. tabs) must exist at the beginning of each content line. Specifically, a space doesn't match a horizontal tab, and vice versa.
- The newline before the closing quotes isn't included in the literal string.
Tip
Visual Studio and the C# Dev Kit provide some validation and syntax highlighting when raw string literals contain JSON data or regular expressions.
The tools parse the text. If the tools have confidence that the text represents JSON or a regular expression, the editor provides syntax coloring.
You can improve that experience by adding a comment above the declaration indicating the format:
// lang=jsonindicates the raw string literal represents JSON data.// lang=regexindicates the raw string literal represents a regular expression.
When a raw string literal is used as an argument where the parameter uses the System.Diagnostics.CodeAnalysis.StringSyntaxAttribute to indicate a format, these tools validate the raw string literal for some of the format types. Both JSON and regex are supported.
For some formats, the comment or the attribute enables code suggestions offer fixes for string literals based on the format.
You might 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.
The raw string literal's content must not contain a set of contiguous " characters whose length is equal to or greater than the raw string literal delimiter length. For example, the strings """" """ """" and """"""" """""" """"" """" """ """"""" are well-formed. However, the strings """ """ """ and """ """" """ are ill-formed
Raw string literals were introduced in C# 11.