Interpolated strings from regex to string

I have a line that looks like this:

string foobar = "foo{bar}";
string bar= "bar";
Console.WriteLine(foobar);

      

is there a fine way to make foobar an interpolated string after it has been declared?

I know I can do this, but I am not getting foobar as an interpolated string

string bar= "bar";
string foobar= $"foo{bar}";
Console.WriteLine(foobar);

      

or like this, but I'm wondering if there is a nicer way to do this

string foobar = "foo{bar}";
string bar= "bar";
Console.WriteLine(foobar.Replace("{bar}", bar);

      

+3


source to share


1 answer


String interpolation is compile time only because it depends on the presence of reference variables.

https://weblogs.asp.net/bleroy/c-6-string-interpolation-is-not-a-templating-engine-and-it-s-not-the-new-string-format



Use something like this:

var foo = "foo{0}";
var bar = "bar;
Console.WriteLine(String.Format(foo, bar));

      

0


source







All Articles