Why is there no string.Format Extension Method?
November 25, 2010
Why is there no built-in extension method like this?
public static string FormatOutput(this string format, params object[] args)
{
return string.Format(format, args);
}
Now the following code
var output = string.Format("This guy's name is {0}", person.Name);
becomes this
var output = "This guy's name is {0}".FormatOutput(person.Name);
Which is much neater in my opinion. Unfortunately we can’t name it ‘Format’ since the compiler gets confused between our extension method and the static method on the string class.
Tags: C#
That is why you need to build up your own little utility library. All these methods belong in there. Just remember to always update the library with new releases of .NET, otherwise you end up with utilities in the util lib that does the same than the .NET framework does.