#16 – Use an Array of Objects for a Composite Format String
July 3, 2010 Leave a comment
Instead of passing in a long list of format items when calling a method like String.Format that deals with composite format strings, you can also just pass in an array of objects. Here’s an example:
object[] args = { "Sean", 36, "Sexton", "C#" }; string sTest = string.Format("{0} {2} has been programming for {1} yrs and loves {3}.", args);
The resulting string here would be: Sean Sexton has been programming for 36 yrs and loves C#.