C# 11: Raw string literal (use arbitrary text in string)

Jan 31, 2023
< >

Easy usage of arbitrary text in a string literal.

Structure:

  • Start with at least """ (3x)
  • (Optional) new line
  • String content with arbitrary text
  • End with the same amount of double quotes
// Without variable  
Console.WriteLine("""  
{ "name": "Georg"} 
"""); 
// { "name": "Georg"}   

// With variable  
const string name = "Georg";  
Console.WriteLine($$"""{ "name": "{{name}}"} """);  
// { "name": "Georg"}

If the string content should contain more than three double quotes you have to increment the start and end part with respective more double quotes.

// Use double quotes  
Console.WriteLine(""""{ """name""": ""Georg""} """");
</ >
Impressum