schulmaster schulmaster -4 years ago 157
C# Question

Escape Character Associativity C# 6 String Interpolation

Given:

double price = 5.05;
Console.Write($"{{Price = {price:C}}}");


and the desired output: {Price = $5.05}

Is there any way to associate the last two curly braces as an escaped '}' so the interpolation works as intended? As it stands, the first two are escaped(I assume?), and the output is :{Price = C}

Console.Write($"{{Price = {price:C} }}");


works as expected, but with the extra space. And I can concatenate the tail brace, which I consider a poor man's solution. Is there a colloquial rich man's solution? Thanks.

Answer Source

You can interpolate instead of concatenate - pass it as a string literal:

double price = 5.05;
Console.Write($"{{Price = {price:C}{"}"}");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download