0
var pattern = getPattern(); //read from the config, something like "{x} abc {y}"

var x = $pattern; //not compiling

Is there a way to convert a pattern in string Interpolated string?

zs2020
  • 53,766
  • 29
  • 154
  • 219
  • 6
    Strings are interpolated at compile time. What you request is just not possible – Wiktor Zychla Jan 06 '23 at 20:17
  • 2
    `string.Format(pattern, x, y)` – T.S. Jan 06 '23 at 20:20
  • The naming interpolated string may be the problem here so that you think that what you try is possible. It is rather interpolated **literal** string. It works on string literals not on strings as such. – Ralf Jan 06 '23 at 20:34
  • You might be interested in a library like [SmartFormat.NET](https://stackoverflow.com/a/36759806/2791540) which could do this for you at run-time. – John Wu Jan 06 '23 at 21:24
  • 2
    You will need to use what @T.S. wrote. And instead of `"{x} abc {y}"` something like `"{0} abc {1}"`. When you do `$"{x} abc {y}"` you are getting the values right there and placing them in the string, you can't just convert a `"{x} abc {y}"` string to magically get the values from the current scope like in your code. What if there's no `x` or no `y`? It would make no sense. – Andrew Jan 06 '23 at 23:09

0 Answers0