0

Has anyone figured out how to reuse an interpolated string?

I.e. can anyone figure out how to do away with the string.Format in the following block of code?

foreach(var s in new[]{ "Primary_{0}_Home", "Secondary_{0}_Work" }){
    sql = $"SELECT {string.format(s, "Street")}, {string.format(s, "City")} ..."
}
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
Rabbi
  • 4,622
  • 9
  • 35
  • 45
  • it is not possible to do this. Reason: the interpolated values in the string are not necessarily in scope. Stick with `string.Format()` for this, it's a lot better! – Philippe Paré Oct 30 '15 at 13:18
  • use `string.Format` http://stackoverflow.com/questions/9354154/how-do-i-interpolate-strings – MethodMan Oct 30 '15 at 13:19

1 Answers1

5

You can't do that.

Interpolated strings are set on compile time. You can't use string interpolation to load a string to format something not directly in the scope.

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325