Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String.Format() - Repassing params but adding more parameters

I would like to do something like that:

public string GetMessage(params object otherValues[]) {
    return String.Format(this.Message, this.FirstValue, otherValues);
}

So, I would like to repass an array of params to String.Format() but adding a new parameter.

What would be the best way to do that, knowing that we could "rebuild" a new array of objects and this doesn't seems good.

like image 821
Luciano Avatar asked May 08 '12 17:05

Luciano


2 Answers

public string GetMessage(params object[] otherValues)
{
    return String.Format(this.Message, new[] { this.FirstValue }.Concat(otherValues).ToArray<object>());
}
like image 84
Tim S. Avatar answered Sep 21 '22 21:09

Tim S.


You could use the Concat and ToArray extension methods:

public string GetMessage(params object[] otherValues) 
{
    var values = new[] { this.FirstName }.Concat(otherValues).ToArray();
    return String.Format(this.Message, values);
}
like image 24
Darin Dimitrov Avatar answered Sep 21 '22 21:09

Darin Dimitrov