C# – How to Use String.format Extension Method

cextension-method

With the addtion of Extension methods to C# we've seen a lot of them crop up in our group. One debate revolves around extension methods like this one:

public static class StringExt
{
    /// <summary>
    /// Shortcut for string.Format.
    /// </summary>
    /// <param name="str"></param>
    /// <param name="args"></param>
    /// <returns></returns>
    public static string Format(this string str, params object[] args)
    {
        if (str == null) return null;
        return string.Format(str, args);
    }
}

Does this extension method break any programming best practices that you can name? Would you use it anyway, if not why? If I renamed the function to "F" but left the xml comments would that be epic fail or just a wonderful savings of keystrokes?

Best Answer

It seems pretty pointless to me.

It's more code that you have to maintain - and in a year's time no one will remember the debates you had and half your code will use string.Format the other half will use this method.

It's also distracting you from the real task of developing software that solves real problems.

You've probably wasted more money by having these discussions that it would have cost the company to buy ReSharper licenses for everyone.