Blog

C# 11 - Raw string literals

With the release of .NET 7 next November, a new version of C# will also be released. This will be the 11th version of the C# language and adds some nice features for developers. In this article I would like to talk about one feature in specific: raw string literals. I think it is a very nice and welcome addition to the language. Let’s find out what it is and why I think it is a good addition.

The problem in the current C# version using strings

Almost every developer sometimes has to work with a string containing special characters that need to be escaped. For a short string this is not a big problem but when you have to deal with larger strings or complete JSON or XML strings it can be very hard to escape all the characters and keep it readable. Also changing data in those kind of strings can be hard. A string would for example look as follows:

var myJson = "{ Property1: \"Value1\", Property2: \"Value2\" }";

In the current version of C# you can already solve this partly by using the @ character in front of the string. When you do that you can use special characters like a backslash (\) without the need to escape it. The only problem is that you still have to escape double quotes by using 2 double quotes. This looks as the following.

var myString = @"I can use a \ in this string but a "" needs to be escaped";

Next to the examples above it is also not possible to put a string on multiple lines. So here we have some reasons why raw string literals are introduced in C# 11. Inside a raw string literal you can just use the special characters (for example a double quote (")) without the need to escape them with a backslash (\) or a double quote ("). Next to that you can also use multiple lines.

How is this solved using raw string literals?

In C# 11 you can start a raw string literal by typing at least 3 double quotes ("""). The raw string literal ends when you use 3 double quotes again. Inside the raw string literal you can use the double quotes and other special characters without the need to escape them. The following code shows how this can be done.

var myJson = """
{
    Property1: "Value1",
    Property2: "Value2"
}
""";

As I said earlier you need to use at least 3 double quotes. If you have a case where you need to use 3 double quotes together you can just add an extra double quote to the beginning and end of the raw string literal. In that way the C# compiler will know that the raw string literal will end when you use the same amount of double quotes again. In between them you can use double quotes without escaping them. Another advantage is that you can use multiple lines for the string. The following example shows this behavior.

var myString = """"
    Here you see 3 double quotes """ specified inside the raw string literal    
"""";

What about string interpolation?

So now I hear you thinking what about string interpolation? Well the simple answer is that this just works. If you put an $ in front of the raw string literal you can just use curly brackets ({) to put variables into the string. The following code shows this.

int numberOfRecords = 13;
var myString = $"""
    There are "{numberOfRecords}" items in the database.
""";

But then there is of course the case that you want to use curly brackets inside your string (for example when you have some json data). To make that possible you can specify multiple $ characters in front of the raw string literal. The number of $ characters specifies how many curly brackets you need to use for string interpolation. In that way you can just use curly brackets inside the string and combine it with string interpolation. The following example shows how you can use 2 $ characters, use curly brackets inside the string and also make use of the string interpolation using 2 curly brackets.

var myJson = $$"""
{
    Property1: "{{value}}",
    Property2: "Value2"
}
""";

Scenarios where this can be useful

In every project I work on I test my code with unittests. In unittest it can often be the case that you have to test some functionality that expects some JSON or XML as input. In those unittest you then specify this JSON or XML as a string. This is a scenario where this feature can be very useful. It makes the code for the unittests more readable and easier to maintain.

The same goes for a project where SQL was dynamically generated based on strings (and yes, there where good checks to avoid SQL-injection :)). The SQL code inside the string would be much more readable by using multiple lines and not escaping all kind of characters. Raw string literals would have been very useful.

Conclusion

Looking at the above examples I think raw string literals is a very nice and handy feature that will be added in C# 11. It will make your life as a developer easier and does not add a lot of complexity to the language. My advice would be to make use of this as soon you can use C# 11 in your project by the end of this year.

Resources:

https://devblogs.microsoft.com/dotnet/csharp-11-preview-updates/#raw-string-literals