

C Sharp 11 Raw String Literals
source link: https://bizzeh.com/c-sharp-11-raw-string-literals/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

C Sharp 11 Raw String Literals
posted Mon, Apr 25, 2022 by darren
If you work with literal strings that can contain quotes or language strings such as JSON, XML, HTML, SQL or Regex, raw literal strings may be the best new feature of C# 11.
If you previously copied a literal string with quotes into a C# literal string, the string ended at the first double quote with compiler errors until you escaped each of the double quotes. If you copied text with curly braces into an interpolated string literal, each curly bracket was interpreted as the beginning of nested code unless you escape each curly bracket by doubling each curly bracket.
Raw string literals have no escaping. A backslash is output as a backslash, a curly bracket is output as a curly bracket and \n is output as the backslash and an n, not a new line.
Raw string literals start and end with at least three double quotes ("""...""")
. 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, just open and close the raw string literal with at least one more quote than that sequence.
Raw string literals can be interpolated by preceding them with a $
. The number of $
’s that prefix the string is the number of curly brackets that are required to indicate the nested code expression. This means that a $
behaves like the existing string interpolation – a single set of curly brackets indicate nested code. 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. Just like with quotes, you can add more $ to allow more curly brackets to be treated as content.
Read more in the raw string literals documentation.
Recommend
-
108
-
77
-
35
-
49
-
65
JEP 326 Raw string literals provide a new way to declare strings without escape characters or unicode escapes. Let's take a look at the example below that declares mult...
-
11
Raw string literals in C# New in C# 11, there is a feature that I think will become highly popular. That feature is raw string literals. There have been a few different modifiers for string literals before now that work in v...
-
26
Microsoft unveils C# 11 list patterns, raw string literals Next version of Microsoft’s flagship programming language for .NET development will also simplify the synt...
-
18
C# 11 Preview Updates – Raw string literals, UTF-8 and more! Kathleen D April 18th, 2022 Features for C# 11 are coming al...
-
17
Use Raw String Literals to generate TwiML in C# 11
-
26
Introducing C#11: Raw string literals 2022-11-29 by anthonygiretti Introduction
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK