Raw string literal feature in C# 11

Raw string literal feature in C# 11

Working with string data types has been improving gradually in different versions of C#. At first glance, they may seem like small changes though they help us in the way that how to deal with strings subtly and usefully. Verbatim string(code>@) and string interpolation ($) are a few of those facilities that C# offers during these years. Along those lines, in this short article, we’re going to look over the raw string literal in C# 11.

If you work with JSON, XML, HTML, SQL, Regex, and others in C#, this feature is the right choice for you.

Prior C# 11

Before C# 11, if you want to have a JSON in your code, you may end up with something like this:

string jsonVariable = "{\"FirstName\":\"MyName\", \"LastName\": \"MyFamily\"}";

Console.WriteLine(jsonVariable);
//output:
    //{"FirstName":"MyName", "LastName": "MyFamily"}

Or thanks to verbatim string, your code would be somthing like this:

string jsonVariable = @"
    {
        ""FirstName"": ""MyName"", 
        ""LastName"": ""MyFamily""
    }";

Console.WriteLine(jsonVariable);
//output:
    //{
    //    "FirstName": "MyName",
    //    "LastName": "MyFamily"
    //}

It’s more readable than the first one, but what if we want to use string interpolation in the JSON string?! In the previous examples, it’s not allowed to use string interpolation, otherwise, it leads to a compilation error due to the curly brackets of the JSON.

However, there is another way to do so which is not wise of course:

string firstName = "MyName";
string lastName = "MyFamily";

string jsonVariable = 
    "{ \n"
    +
        $" \"FirstName\" : \"{firstName}\", \n"
        +
        $" \"LastName\" : \"{lastName}\", \n" 
    +
    "}";

Console.WriteLine(jsonVariable);
//output:
    //{ 
    // "FirstName" : "MyName", 
    // "LastName" : "MyFamily", 
    //}

Let’s see how the raw string literal can rescue us.

Raw string literal

The raw string literal is a new feature to support multi-line string that can be interpolated by $. Also you can add even extra double quotes or curly brackets in your string.

string firstName = "MyName";
string lastName = "MyFamily";

var jsonVariable =
$$"""
    {
        "FirstName": "{{firstName}}",
        "LastName": "{{lastName}}"
    }
""";

Console.WriteLine(jsonVariable);
//output:
    //{
    //    "FirstName": "MyName",
    //    "LastName": "MyFamily"
    //}

When you want to use C# 11, since it’s in the preview version, don’t forget to use the EnablePreviewFeatures tag in your .csproj file.

The raw string literal $$""" ... """ starts with at least two $ and three ", also ends with three ". As you can see, it’s very handful.

Number of $

The number of $ that prefixes the string equals the number of curly brackets that are required to indicate a nested code expression. It means that if there are two $, we need to use two curly brackets to specify the interpolated part of a string, likewise, for three $, three curly brackets are required.

For example:

string firstName = "MyName";
string lastName = "MyFamily";

var jsonVariable =
$$$"""
    {{// two curly brackets can be printed because of three $
        "FirstName": "{{{firstName}}}",
        "LastName": "{{{lastName}}}"
    }
""";

Console.WriteLine(jsonVariable);
//output:
    //{{
    //    "FirstName": "MyName",
    //    "LastName": "MyFamily"
    //}

Note that, the number of curly brackets that can be printed in the output is the number of $ that prefixes the string minus one.

Number of "

The same rule is here for ". As I mentioned, the raw string literal starts with three " and the number of quotes that can be in the output is the number of prefix quotes minus one which means that in the previous example, we could show "" in the output.

Let’s see three double quotes in the output:
To do that first we need to stat the raw string literal with four double quotes. """"

string firstName = "MyName";
string lastName = "MyFamily";

var jsonVariable =
$$""""
    {
        """FirstName""": "{{firstName}}",
        """LastName""": "{{lastName}}"
    }
"""";

Console.WriteLine(jsonVariable);
//output:
//    {
//        """FirstName""": "MyName",
//        """LastName""": "MyFamily"
//    }

Sum up

In this short post, we see how to use the raw string literal in C# 11 which is useful for cases like JSON strings which need multi-line strings and support interpolation. It also improves our code in terms of readability and maintenance.

Who am I?

I’m Kevin, a Backe-end developer in Techspire, passionate about discovering new technologies and very focused on .NET products. Outside of my work at Techspire, I consume many blog posts, podcasts, and videos to learn and am happy to share them with people.