If you don't have C#6 available in your project you can use Linq's .Aggregate():
var str = "Her name is @name and she's @age years old";
var parameters = new Dictionary<string, object>();
parameters.Add("@name", "Lisa");
parameters.Add("@age", 10);
str = parameters.Aggregate(str, (current, parameter)=> current.Replace(parameter.Key, parameter.Value.ToString()));
If you want something matching the specific syntax in the question you can put together a pretty simple class based on Aggregate:
public class StringFormatter{
public string Str {get;set;}
public Dictionary<string, object> Parameters {get;set;}
public StringFormatter(string p_str){
Str = p_str;
Parameters = new Dictionary<string, object>();
}
public void Add(string key, object val){
Parameters.Add(key, val);
}
public override string ToString(){
return Parameters.Aggregate(Str, (current, parameter)=> current.Replace(parameter.Key, parameter.Value.ToString()));
}
}
Usable like:
var str = new StringFormatter("Her name is @name and she's @age years old");
str.Add("@name", "Lisa");
str.Add("@age", 10);
Console.WriteLine(str);
Note that this is clean-looking code that's geared to being easy-to-understand over performance.