Tuesday, April 26, 2022

C# 11.0: Raw String Literals

In this post, let's have a look at one of the nicest features coming in C# 11.0. And this is one of my favorites.

You can also try out this feature now by setting the LangVersion to preview in your .csproj file.
<LangVersion>preview</LangVersion>
Say you need to declare a variable with a JSON string, something like this.
{
  "name""John Doe",
  "address": {
    "addressLine1""Address Line 1",
    "addressLine2""Address Line 2",
    "city""City",
    "state""State",
    "postalCode""12345-6789",
    "country""Country"
  }
}
And prior to C# 11.0, in order to get this into a variable, we need to modify the JSON string to escape the double-quotes.
string jsonString =
    @"{
    ""name"": ""John Doe"",
    ""address"": {
        ""addressLine1"": ""Address Line 1"",
        ""addressLine2"": ""Address Line 2"",
        ""city"": ""City"",
        ""state"": ""State"",
        ""postalCode"": ""12345-6789"",
        ""country"": ""Country""
    }
}";
And now say you want to use string interpolation for some of the values. And for that you need to escape the curly braces, something like this.
string name = "John Doe";
string jsonString =
    @$"{{
    ""name"": ""{name}"",
    ""address"": {{
        ""addressLine1"": ""Address Line 1"",
        ""addressLine2"": ""Address Line 2"",
        ""city"": ""City"",
        ""state"": ""State"",
        ""postalCode"": ""12345-6789"",
        ""country"": ""Country""
    }}
}}";
And that's a lot of work.

With Raw String Literals in C# 11.0, you can do something like below.
string name = "John Doe";
string jsonString =
    $$"""
    {
        "name": "{{name}}",
        "address": {
            "addressLine1": "Address Line 1",
            "addressLine2": "Address Line 2",
            "city": "City",
            "state": "State",
            "postalCode": "12345-6789",
            "country": "Country"
        }
    }
    """;
And note, here I didn't escape double quotes nor the curly braces. I only had to change the value of the name property to use the string interpolation. So basically it's just copying and pasting the JSON as it is and doing a minor change if we are using string interpolation which we will have to do anyway.

A couple of important notes here:
  • Raw string literals start and end with at least three double-quotes.
string jsonString =
    """
    {
        "name": "John Doe",
    }
    """;
  • Within these double quotes, single " are considered content and included in the string
  • Any number of double quotes less than the number that opened the raw string literal are treated as content. So, in the common case of three double quotes opening the raw string literals, two double quotes appearing together would just be content.
  • If you need to output a sequence of three or more double-quotes, then open and close the raw string literal with at least one more quote than that sequence, something like below.
string jsonString =
    """"
    {
        "name": "John Doe",
        "description": "Some Description with """ quotes "
    }
    """";
  • Raw string literals can be interpolated by preceding them with a $. The number of $ that prefixes the string is the number of curly brackets that are required to indicate a nested code expression.
string name = "John Doe";
string someString = $""" His name is "{name}".""";
  • If a raw string literal is prefixed with $$, a single curly bracket is treated as content and it takes two curly brackets to indicate nested code (as shown in the jsonString with string interpolation code snippet above). Just like with quotes, you can add more $ to allow more curly brackets to be treated as content.
Hope this helps.

Happy Coding.

Regards,
Jaliya

No comments:

Post a Comment