How to enter quote characters in C # 6.0 string interpolations

Considering

IDictionary<string, string> x;

      

earlier you could do (as example code for a parameter having quotation marks):

string.Format("{0}", x["y"]);

      

What is the correct way to format 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

      

Clearing quotes like

\"

      

does not work and does

 var b = "y";
 ...
 $"{x[b]}"

      

seems uncomfortable.

+3


source to share


1 answer


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 version 6.0 of the C # language level (this is the default option on VS2015).

Edit: you can also try here . (make sure you check "C # 6.0 Beta").

+7


source







All Articles