String.Format and StringBuilder (via the AppendFormat method) allow callers to pump values into a string they have prepared, e.g:
string temp = string.Format("Item {0} of {1}, Record Id: {2} started...",
itemCounter.ToString(),
totalItemsToProcess.ToString(),
myRecord.RecordId);
MyMethod(temp);
But rather than build a string and pass that into "MyMethod()" I'd rather have an overload that people called like this:
MyMethod("Item {0} of {1}, Record Id: {2} started...",
itemCounter.ToString(),
totalItemsToProcess.ToString(),
myRecord.RecordId);
How would you implement that? Is there something I can leverage or do I have to write a bunch of custom code?
Overview. String formatting uses a process of string interpolation (variable substitution) to evaluate a string literal containing one or more placeholders, yielding a result in which the placeholders are replaced with their corresponding values.
It's pretty trivial, but there are less trivial uses of params:
static string MyMethod( string format, params object[] paramList )
{
return string.Format(format, paramList);
}
How about params? http://msdn.microsoft.com/en-us/library/w5zay9db.aspx
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With