4

I have been reading this SO post carefully, trying to put curly brace literals around my interpolated strings.

string testString = "foo";
string testResult1 = $"{testString}"; // result = "foo" as expected
string testResult2 = $"{{testString}}"; // result = "{testString}" - UH OH

My expected result for testResult2 is "{foo}". I have tried escaping the outer curlies with backslash, but that doesn't work, and I didn't expect it to. How can I put literal curly braces around an interpolated string variable? A more accurate example is this:

string testResult3 = $"I want to eat some {{testString}} please.";

Expected: "I want to eat some {foo} please."

Actual: "I want to eat some {testString} please."

How can I make this work? (I also tried @ between $ and ", but no joy.)

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158

1 Answers1

6

From Chris R. Timmons:

Two curly braces evaluate to a literal curly brace. Therefore you need three curly braces:

string testResult2 = $"{{{testString}}}";

...to produce {foo}.

HerrimanCoder
  • 6,835
  • 24
  • 78
  • 158