String Interpolation with format variable

No, you can’t use string interpolation with something other than a string literal as the compiler creates a “regular” format string even when you use string interpolation.

Because this:

string name = "bar";
string result = $"{name}";

is compiled into this:

string name = "bar";
string result = string.Format("{0}", name);

the string in runtime must be a “regular” format string and not the string interpolation equivalent.

You can use the plain old String.Format instead.

Leave a Comment