Raw string literals are a new feature in C# that allows developers to create strings that are simpler to read, write, and manipulate. Raw string literals are made up of a series of characters surrounded by double quotes, but the ‘at’ character (@) comes before the opening quotes, rendering them readable even if they contain special characters or spaces.
- Raw string literals
- Generic math support
- Generic attributes
- UTF-8 string literals
Special characters such as the newline (n) and tab (t) must be expressed by escape sequences in traditional strings. If you wanted to make a string that starts with “Hello” and ends with a newline character, you would usually type:
string message = "Hello\nEveryone";
Raw string literals
Using special characters, as in the traditional approach, is no longer required with raw string literals. You could write the above conventional string using raw string literals as follows:
string message = @"Hello Everyone";
This syntax is not only less verbose, but also more readable, particularly when dealing with long strings or strings containing a lot of special characters. Raw string literals also support multi-line strings, so you can make strings that span multiple lines. When creating templates, formatting text, or writing code that produces markup, this can be extremely helpful.
To make a multi-line raw string literal, add a new line after the starting quotation marks and indent the string’s content relative to the quotation marks. For example, to make a multi-line string containing an HTML block, write:
string html = @""
Raw string literals also allow you to include quotations in your strings without having to escape them. This is especially helpful when you need to make strings with embedded quotes, such as when writing SQL queries or other types of markup. To include a quotation in a raw string literal, use two quotes in a row. For instance:
string sql = @" SELECT * FROM Customers WHERE Name = 'it's apple' ";
The single quote in the name “it’s apple” is escaped in this case by doubling it. This indicates that you meant to include a single literal quote in your string rather than ending it prematurely.
Features of raw string literals in C#
Support for Interpolation
String interpolation is one of the most important aspects of raw string literals in C#. This means that without turning to concatenation, developers can simply include variables and expressions within the raw string. Interpolation is accomplished by placing the $ symbol before the raw string’s beginning quote.
Another benefit of using raw string literals is that line breaks can be included in the string. Line breaks in a normal string literal would be represented by the escape character ‘n’. Line breaks, on the other hand, can be included in a raw string literal by merely 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: \.
Another significant advantage of raw string literals is that they allow developers to easily build multi-line strings. This is due to the string’s ability to span numerous lines without the use of any special characters. Each line of the string would need to be separated using the escape character ‘\n’ in a normal string literal.