3

I have a string that looks like this:

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

is there a pretty way of making foobar a interpolated string after it has been declared?

I know I can do like this, but i don't receive foobar as a interpolated string

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

or like this, but I wonder if there's a prettier way of doing it

string foobar = "foo{bar}";
string bar= "bar";
Console.WriteLine(foobar.Replace("{bar}", bar);
2
  • 5
    String interpolation is a compile time language feature. You want to use it as a runtime feature. You can use String.Format. Commented Apr 26, 2017 at 7:39
  • This has already been answered and can't be done Commented Apr 26, 2017 at 7:47

1 Answer 1

0

String Interpolation is compile time only because it relies on the presence of the referenced 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 instead:

var foo = "foo{0}";
var bar = "bar;
Console.WriteLine(String.Format(foo, bar));
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.