The problem is that string interpolation in C# is just a syntactic sugar wrapper for string.Format
. The strings get compiled to the same code. Take this code for example:
public string GetGreeting1(string name)
{
return string.Format("Hello {0}!", name);
}
public string GetGreeting2(string name)
{
return $"Hello {name}!";
}
The IL output for both methods is identical:
GetGreeting1:
IL_0000: ldstr "Hello {0}!"
IL_0005: ldarg.1
IL_0006: call System.String.Format
IL_000B: ret
GetGreeting2:
IL_0000: ldstr "Hello {0}!"
IL_0005: ldarg.1
IL_0006: call System.String.Format
IL_000B: ret
So the solution you have is perfectly valid and one I've used before. The only possible improvement would be if you intend on doing lots of replacements, you may find a performance benefit if you switch to using a StringBuilder
. You could probably find a library on Nuget that does it for you, but that might just be overkill for your purpose.
As an extra, I made an extension class for this. With an extra bonus I've also added a method for using an object with any number of parameters that uses reflection to do the replacements:
public static class StringReplacementExtensions
{
public static string Replace(this string source, Dictionary<string, string> values)
{
return values.Aggregate(
source,
(current, parameter) => current
.Replace($"{{{parameter.Key}}}", parameter.Value));
}
public static string Replace(this string source, object values)
{
var properties = values.GetType().GetProperties();
foreach (var property in properties)
{
source = source.Replace(
$"{{{property.Name}}}",
property.GetValue(values).ToString());
}
return source;
}
}
And use like this:
var source = "Hello {name}!";
//Using Dictionary:
var dict = new Dictionary<string, string> { { "name", "David" } };
var greeting1 = source.Replace(dict);
Console.WriteLine(greeting1);
//Using an anonymous object:
var greeting2 = source.Replace(new { name = "David" });
Console.WriteLine(greeting2);