17

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.

Felix Castor
  • 1,598
  • 1
  • 18
  • 39

1 Answers1

35

Type { twice to escape it:

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

BTW you can do the same with double quotes.

Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459