C# tutorials > Modern C# Features > C# 6.0 and Later > Explain raw string literals in C# 11 and their use cases.
Explain raw string literals in C# 11 and their use cases.
Understanding Raw String Literals in C# 11
C# 11 introduced raw string literals, a powerful feature that simplifies working with strings containing special characters, embedded quotes, or multi-line content. This tutorial explores raw string literals, their syntax, use cases, and advantages over traditional string literals.
Introduction to Raw String Literals
Raw string literals are a new way to define strings in C# 11. They are delimited by at least three double-quote characters ("""
) and can span multiple lines without requiring escape sequences for quotes or other special characters. This makes them ideal for scenarios like embedding JSON, XML, or code snippets directly into your C# code.
Basic Syntax
The most basic syntax involves wrapping the string content within three or more double quotes. The number of double quotes used to begin the raw string literal must be matched at the end. Leading whitespace is removed based on the closing quotes indentation, making it easier to format the string within your code.
string rawString = """
{
"name": "John Doe",
"age": 30
}
""";
Console.WriteLine(rawString);
Handling Leading Whitespace
The compiler automatically removes common leading whitespace based on the position of the closing delimiter. In the above example, the minimal indentation level on the closing """
is used to remove indentation from all lines within the string. This ensures clean string output without manual trimming.
string rawString = """
{
"name": "John Doe",
"age": 30
}
""";
Console.WriteLine(rawString);
Embedding Double Quotes
If you need to include sequences of double quotes within a raw string literal, you can use more than three double quotes as delimiters. The starting and ending sequences must match. In this example, we use six double quotes to allow for the inclusion of single double quotes within the string.
string rawString = """"""
This string contains "double quotes".
"""";
Console.WriteLine(rawString);
Use Case: Embedding JSON
Raw string literals are exceptionally useful for embedding JSON data within your code. Without raw string literals, you would need to escape the inner double quotes, making the code harder to read. This example demonstrates embedding a simple JSON structure.
string jsonString = """
{
"name": "Alice",
"age": 25,
"city": "New York"
}
""";
Console.WriteLine(jsonString);
Use Case: Embedding XML
Similar to JSON, raw string literals simplify embedding XML content. The absence of escape characters improves readability and maintainability.
string xmlString = """
<book>
<title>The Lord of the Rings</title>
<author>J.R.R. Tolkien</author>
</book>
""";
Console.WriteLine(xmlString);
Use Case: Embedding Code Snippets
Embedding code snippets, whether C# or other languages, becomes straightforward with raw string literals. The raw string literal preserves formatting and makes it easy to include complex code blocks.
string codeSnippet = """
public class Example
{
public static void Main(string[] args)
{
Console.WriteLine("Hello, World!");
}
}
""";
Console.WriteLine(codeSnippet);
When to Use Raw String Literals
Raw string literals are best suited for:
Alternatives to Raw String Literals
Before C# 11, you would typically use either regular string literals with escape sequences or verbatim string literals ( For example, the equivalent of the JSON example using a verbatim string literal would be: This is less readable than the raw string literal version.@"..."
). Verbatim string literals avoid escaping backslashes but still require escaping double quotes.string jsonString = @"{
""name"": "Alice",
""age"": 25,
""city"": "New York"
}";
Pros of Raw String Literals
Cons of Raw String Literals
Best Practices
FAQ
-
What is the minimum C# version required to use raw string literals?
Raw string literals are available in C# 11 and later. -
How do I include double quotes within a raw string literal?
Use more than three double quotes to delimit the string. For example, if you need to include a single double quote, use six double quotes as delimiters (""""""...""""""
). -
How does leading whitespace work in raw string literals?
The compiler removes common leading whitespace based on the indentation of the closing delimiter. The minimal indentation level is removed from all lines within the string.