Raw String Literals In C# 11

What is C# 11.0?

C# 11.0 is the latest version of the C# language.

Features

  • Raw string literals
  • Generic math support
  • Generic attributes
  • UTF-8 string literals
  • etc

Introduction

Raw string literals in C# 11 are a new feature for developers to create strings that are easier to read and write and manipulate. Raw string literals comprise a sequence of characters enclosed in double quotes, but the 'at' character (@) comes before the opening quotes, making them readable even if they contain special characters or spaces.

Traditional strings

In traditional strings, special characters like the newline (\n) or the tab (\t) must be represented by escape sequences. If you wanted to create a string that contains the word "Hello" followed by a newline character, you would typically write:

string message = "Hello\nWorld";

Raw string literals

With raw string literals, using special characters like the traditional method is no longer necessary. Using raw string literals, you could write the above traditional string like this:

string message = @"Hello
World";

This syntax is not only less verbose, but it's also more readable, especially when dealing with large strings or strings that contain many special characters. Additionally, raw string literals support multi-line strings, allowing you to create strings that span multiple lines. This can be incredibly useful when creating templates, formatting text, or writing code that generates markup.

To create a multi-line raw string literal, you add a new line after the opening quotes and indent the content of the string relative to the opening quotes. For example, to create a multi-line string that contains a block of HTML, you could write:

string html = @""

Raw string literals also let you include quotes in your strings without having to escape them. This is particularly useful when you need to create strings containing embedded quotes, such as when writing SQL queries or other kinds of markup. To include a quote in a raw string literal, use two quotes in a row. For example:

string sql = @"
SELECT *
FROM Customers
WHERE Name = 'la pinoz''s Pizza'
";

In this example, the single quote in the name "la pinoz's Pizza" is escaped by doubling it up. This makes it clear that you intended to include a single literal quote in your string rather than terminating the string prematurely.

Features of raw string literals in C# 11.0

Support for Interpolation

One of the main features of raw string literals in C# 11 is the support for string interpolation. This means that developers can easily include variables and expressions within the raw string without resorting to concatenation. Interpolation is achieved by using the $ symbol before the opening quote of the raw string.

Line Breaks

Another advantage of using raw string literals is that they allow line breaks to be included as part of the string. With a regular string literal, line breaks would be represented using the escape character '\n'. However, with a raw string literal, line breaks can be included simply by pressing the enter key.

Easy Escaping of Characters

Escaping characters in a regular string literal can be a bit of a hassle. This is because certain characters, such as \ (backslash), need to be escaped using another backslash. However, with raw string literals, escaping is not necessary for most characters. For instance, if you want to include a backslash in a raw string, you can simply type it twice, like this: \.

Multi-Line Strings

Another major benefit of raw string literals is that they enable developers to create multi-line strings easily. This is because the string can span multiple lines without requiring any special characters. In a regular string literal, each line of the string would need to be separated using the escape character '\n'.

Conclusion

Raw string literals are a convenient and useful new feature in C# 11 that can make your code more readable, easier to write, and less error-prone. By freeing you from the need to use escape sequences, you can focus on the content of your strings rather than worrying about formatting and parsing. Whether you're working with simple strings, complex templates, or multi-line markup, raw string literals are a powerful tool that can save you time and effort while improving the quality and maintainability of your code.