NDepend

Improve your .NET code quality with NDepend

C# 11 Raw String Literals Explained

C# 11 introduces Raw String Literals. Undoubtedly this feature will become very popular because it represents an elegant way to solve some issues with actual string literal.

Let’s have a look at such raw string literal with interpolation. Notice that a raw string literal necessarily starts and ends with at least 3x double quote characters """.

We use console coloring to highlight the string obtained at runtime:

Console-C#11-Raw-Literal-Strings

We can see that C# 11 raw string literal has three major impacts on:

  • Formatting and Alignment: for example there are 6 white spaces before the character { in the literal that are skipped by the compiler.
  • Double quote character " that so far couldn’t be declared as-is in a string literal. To obtain the 3 characters string -"- we had to either use this literal "-\"-" or this one @"-""-".
  • Interpolation: this string literal is relying on string interpolation but interpolation delimiters are {{ and }} and the single character { is declared as-is, which was not possible so far in an interpolated string.

Let’s detail these three various impacts one by one:

Formatting and Alignment in Raw String Literals

Let’s slightly modify the small program above:

  • First we remove a space just before the """ end delimiter. In the output we can see that the string resulting from compilation is aligned with the end delimiter column. We can also see that the Visual Studio editor shows a vertical gray line that indicates what the string resulting from compilation will be.
  • Second we add a line before the """ end delimiter line and it contains a single whitespace (selected in code for clarity). Despite being made of only whitespaces, this extra line is included in the string resulting from compilation.

Clearly C# 11 raw string literal is a great way to declare much clearer JSON, XML, SQL, test expectations… string literals in code thanks to alignment with the actual nesting:

clearer alignement

Let’s challenge this new string literal alignment feature. First only whitespace characters are accepted in columns before the column of the """ end delimiter

Raw Literal Strings whitespace characters

Second the begin and end delimiter’ lines can only accept whitespace characters. These whitespace characters are skipped by the compiler. Let’s underline that delimiters can be on the same line for single line literals: """this is a single line raw string literal""".

Raw String Delimiter must be on its own line

Sequences of double quote ” characters in Raw String Literals

A raw string literal necessarily starts and ends with at least 3x double quote characters """. At least is in bold because more than three double quote characters can be used for delimiters. With the """ delimiter the literal can contain " and "" sub-strings. With the """" delimiter the literal can contain ", "" and """ sub-strings. And so on…

C#11-Raw-Literal-Strings

This astute of an extra double quote in delimiter is quite nifty if you ask me 🙂: It thoroughly solves the ugly back-slashing \" and double double-quote character "" in string literals.

Here is the plain-text code if needed:

Interestingly enough three consecutive double quote characters """ was never a valid expression in C# 10 and earlier so there is no risk of breaking existing code. Also two consecutive double quote characters "" was (and remains) the expression for the empty string so it couldn’t be used for raw string literal delimiters.

Interpolation in Raw String Literals

The same astute is used for interpolation brace in string literal. In interpolated string so far a { or } brace character required a double braces sequence, {{ or }} . Now the number of leading dollar character $, determines the number of brace characters the literal can contain without being considered as an interpolation expression delimiter. This code gets clearer with the Visual Studio editor highlighting:

C#11-Raw-Literal-Strings-and-Interpolation-Strings

Here is the plain-text code if needed:

Enjoy 🙂🙂🙂

My dad being an early programmer in the 70's, I have been fortunate to switch from playing with Lego, to program my own micro-games, when I was still a kid. Since then I never stop programming.

I graduated in Mathematics and Software engineering. After a decade of C++ programming and consultancy, I got interested in the brand new .NET platform in 2002. I had the chance to write the best-seller book (in French) on .NET and C#, published by O'Reilly and also did manage some academic and professional courses on the platform and C#.

Over my consulting years I built an expertise about the architecture, the evolution and the maintenance challenges of large & complex real-world applications. It seemed like the spaghetti & entangled monolithic legacy concerned every sufficiently large team. As a consequence, I got interested in static code analysis and started the project NDepend.

Today, with more than 12.000 client companies, including many of the Fortune 500 ones, NDepend offers deeper insight and full control on their application to a wide range of professional users around the world.

I live with my wife and our twin kids Léna and Paul in the beautiful island of Mauritius in the Indian Ocean.

Comments:

Leave a Reply

Your email address will not be published.