C#11 Features - Raw String Literals

Raw String Literals are the new format of string literals introduced in C# 11. This new format can help us in getting the output string with any arbitrary text like new lines, spaces, and special characters without any special sequences. 

Earlier, for example, if we need to maintain an email template with huge text with multiple lines and special characters including placeholders need to replace it at runtime before sending the email to the end user. To achieve this requirement, we need to follow some indirect approaches like maintaining these text as part of external embedded files or reading from a database.

Now, using this new raw string literals feature, we can overcome these difficulties and directly declare the template string as a string variable which we use to do for single line strings. Before going for implementation we need to understand the following points.

  • Raw string literals can contain arbitrary text, including whitespace, new lines, embedded quotes, and other special characters without requiring escape sequences.
  • A raw string literal starts with at least three double-quotes (""") characters. It ends with the same number of double-quote characters.
  • Typically, a raw string literal uses three double quotes on a single line to start the string, and three double quotes on a separate line to end the string.
  • The newlines following the opening quote and preceding the closing quote aren't included in the final content.
  • Any whitespace to the left of the closing double quotes will be removed from the string literal.

Following are the sample string variable having a leave request email content. 

string myEmailContent = """ 
     Dear James,
    I would like to request "2 leave days" that start from 11/26/2022 and end on 11/27/2022. 
    """;

When you correlate the given points above with this email content the output string will be without the new lines at the end as well no escape sequence is needed while using double-quote as part of email content. The same will look on the visual studio 2022 as follows:

C#11 features - Raw String Literals

Once we enter three double quotes, immediately a new vertical line can be seen which I highlighted with a yellow line. Whatever content you keep between these will be considered as a raw string. 

Combination with String Interpolation

Ok, till now we are good with the above example where we use to get the email content from the string variable instead of fetching this content from any external sources. When observed, this content is more static and cannot be used as a template meaning, we cannot use the same content forever as we have some static text like name, number of days, and dates.

As we were aware, we can achieve this by placing the placeholders and replacing those at runtime using the string interpolation feature since C# 6. String Interpolation can be achieved by preceding with a symbol and bracing with dynamic variables. Even in raw string literals, we can use the string interpolation with some new style in case we want braces as part of the output string. 

To achieve this, need to place multiple $ symbols based on how many consecutive braces we need as part of the output string. The following are simple examples to understand this new style.

string myEmailContent = $$""" 
     Dear {{_name}}, 
    I would like to request "{{_days}} {leave days}" that start from {{_fromdate}} and end on {{_todate}}. 
    """;

Here, we placed two $ symbols, which means, whenever runtime identified two braces start and ends will consider as a placeholder. But when observing the above example, for leave days we used single braces and runtime doesn't treat it as a placeholder because we preceded with two $ symbols. Similarly, if we need two consecutive braces as part of the output string, we need to have three $ symbols so that we can instruct the runtime to identify placeholders for any consecutive three start and end braces.

You can check my other article here where I provided new features introduced for String Interpolation as part of C# 11.

Happy Coding :) 


Recommended Free Ebook
Similar Articles