Search code examples
c#string-interpolation

escape for "{" inside C# 6 string interpolation


I am hoping to use a "{" inside a string interpolation statement but I'm having trouble finding the escape character to do it.

var val = "ERROR_STATE";
var str = $"if(inErrorState){ send 1,\"{val}\" }"

Desired output:

if(inErrorState){send 1,"ERROR_STATE"}

The simple solution is to just not use string interpolation, but I think this way of doing it is easier to read.


Solution

  • Type { twice to escape it:

    $"if(inErrorState){{send 1, \"{val}\" }}"
    

    BTW you can do the same with double quotes.