Given
IDictionary<string, string> x;
previously you could do (as an example of the parameter code having quote marks):
string.Format("{0}", x["y"]);
What is the proper way to format the C# 6.0 string interpolation?
$"{x["y"]}" // compiler error due to the quotes on the indexer value
// UPDATE: actually does work, must have had another typo I missed
Escaping the quotes as
\"
doesn't work, and doing
var b = "y";
...
$"{x[b]}"
seems awkward.
This works for me:
var dictionary= new Dictionary<string, string>();
dictionary.Add("x","value of x");
Console.WriteLine($"x is {dictionary["x"]}");
Make sure your project is set to use the version 6.0 of C# Language level (it's the default option on VS2015).
Edit: You can also try it here. (make sure you check the "C# 6.0 Beta").
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With