16 November

C# 11 – Raw string literals

min. read

Reading Time: 3 minutes
C# 11
C# 11

Let’s list all the features that C# 11 brings to the table. Let’s discuss them all one by one, with their use cases, and see how they can come in handy. At the bottom of each article, you can find a link to all 14 new C# features!

Raw string literals

Example — explanation in the article

var myJsonRawLiteral = $$"""
{
    "name": "{{myString}}",
    "age": 28,
    "raw_name": {not_from_c#},
}
""";

Previously in C# 10

Previously in C# 10 and before, in order to have a string value that could include " (double quote) sign, it needed to be escaped.

Escape direct string with \

var myString = "I want to have \"quotes\" in the text";

Escape string literal with double quotes “”

var myStringLiteral = @"I want to have ""quotes"" in the text";

Escape string verbatim with \

var myStringWithVerbatim = $"I want to have \"quotes\" in the text {myString}";

Escape string verbatim with literal with double quotes “”

var myVerbatimWithLiteral = @$"I want to have ""quotes"" in the text {myString}";

The task becomes more complex when we have a string that would be required to have quotes and variables. The best example is a JSON string.

JSON string use case

Previously any string that would need to have a double quote and a bracket was a nightmare to use. See the examples below

var myJson = "{\"name\":\"John\",\"age\":30,\"car\":null}";
var myJsonWithVerbatim = $"{{\"name\":\"John\",\"age\":30,\"car\":null}}";
var myJsonWithLiteral = @"{""name"":""John"",""age"":30,""car"":null}";

and when we needed to “inject” variables into the JSON string from the C#

var myName = "John";
var myJsonVerbatim = $"{{\"name\":\"{myName}\",\"age\":30,\"car\":null}}";
var myJsonLiteral = @$"{{""name"":""{myName}"",""age"":30,""car"":null}}";
// String format version
var myJsonAsStringFormat = string.Format("{{\"name\":\"{0}\",\"age\":30,\"car\":null}}", myName);

JSON hack

As you look at the previous examples we can see that all of those are unreadable. This is not yet the end of the world for JSON. If we could create an object, and then serialize it and “waste” a little bit of CPU power we could do something like this that is somewhat similar to JSON syntax.

var myJsonAsObject = new { name = "John", age = 30, car = (string?)null };
var toJsonString = JsonConvert.SerializeObject(myJsonAsObject);

XML and others

But as you can see in other examples below, this is not possible with other languages. Especially, when you want to have a line break.

var myXml = @"<root>
  <child>
    <grandchild />
  </child>
</root>";

var myXmlWithVerbatim = [email protected]"<root>
  <child>
    <grandchild name=""{myString}"" />
  </child>
</root>";

var myYaml = @"name: John
age: 30
";
var myYamlWithVerbatim = [email protected]"name: {myString}
age: 30
";

Raw string literal as a solution

In C# 11, the .NET team added a solution to display all of those strings nicely.

If you start and end your string with at least 3 (three) double quotes, what you get it a new C# 11 feature, called a raw string literal. Here you do not have to escape quotes or add line breaks.

var myRawLiteral = """I want to have "quotes" in the text""";

And that’s it!
Now if we want to include a parameter from C#, then we would start it as any other verbatim with $ sign before.

var myRawVerbatim = $"""I want to have "quotes" in the text {myString}";

Escaping raw string literal

So what about if I want to have my braces { , } or do I have 3 quotes? That’s quite simple. You just denote how many of those you have and append the start with more quotes or add another $ sign.

You can have more than 4 quotes at the start but those must be matching the end of the string.

var myThreeQuotest = """"I want a tripple """ quote without escaping"""";

You can have more than 2 dollar signs at the start but for each, you need to have one more braces for C# to treat it as verbatim.

var variable = 12;
var myBracesExample = $$"""This is {IN_TEXT} and this is {{variable}} from C#"""

JSON, XML and Yaml examples

To finish this article with more examples here are each of those use cases.

JSON

var myString = "Tom";
var myJsonRawLiteral = $$"""
{
    "name": "{{myString}}",
    "age": 28,
    "raw_name": "{not_from_c#}",
}
""";

XML

var myXmlRawLiteral = $$"""
<root>
    <name>{{myString}}</name>
    <age>28</age>
    <raw_name>{not_from_c#}</raw_name>
</root>
""";

YAML

var myYamlRawLiteral = $$"""
root:
    name: {{myString}}
    age: 28
    raw_name: {not_from_c#}
""";

Further reading at dotnet GitHub page.


Author

Tomasz Juszczak

CTO /

Technical Lead

Tomasz Juszczak

About prog

Founded in 2016 in Warsaw, Poland. Prographers mission is to help the world put the sofware to work in new ways, through the delivery of custom tailored 3D and web applications to match the needs of the customers.


Let's talk

SEND THE EMAIL