For example I have a string that was retrieved from the database.
string a = "The quick brown {Split[0]} {Split[1]} the lazy dog";
string b = "jumps over";
And then I will perform this code.
String[] Split= b.Split(' ');
String c= $"{a}";
Console.Writeline(c):
This method is not working. Do you have any idea how can this become possible? I appreciate your help. ^-^
The interpolated strings are interpreted by the compiler. I.e., for example in
... is converted to
... by the compiler.
Therefore, you cannot use string interpolation at runtime with variable names in a (regular) string.
You must use
String.Format
at runtime:Note that a runtime, the names of the local variables are not known. If you decompile a compiled C# programm, the decompiled code will contain generic names for local variables like
l1
,l2
etc. (depending on the decompiler).