Preprocess message
If you do not want to create a new array in each call to GetMessage (...), you can insert FirstValue into the message at the beginning once at a time , and then GetMessage (...) just uses the otherValues parameter for string.Format (. ..).
The Message property is initialized after setting FirstValue, for example. in the constructor or in the init method like this:
void InitMessage() { Message = String.Format(Message, FirstValue, "{0}", "{1}", "{2}", "{3}", "{4}"); }
The InitMessage method initializes the first index in Message with FirstValue, and the remaining indexes with "{index}", that is, "{0}", "{1}", "{2}", ... (This is allowed to have more params
elements than post indices).
Now GetMessage can call String.Format without any array operations:
public string GetMessage(params object[] otherValues) { return String.Format(Message, otherValues); }
Example:
Assume the following property values:
this.Message = "First value is '{0}'. Other values are '{1}' and '{2}'."
and this.FirstValue = "blue"
.
Changes to InitMessage Message to:
"First value is 'blue'. Other values are '{0}' and '{1}'."
.
GetMessage Request
GetMessage("green", "red")
results
"First value is 'blue'. Other values are 'green' and 'red'."
.