C# Extension Method shortcut for Format


Some people hate extension methods used like this, but in my own projects where I’m going to be the only coder, I find it saves me time. I found this one useful. Also, I needed a first post since moving my blog from Wordpress.com to a self installation on my own site. :D

using System;
using System.Linq

public static class StringExtensions
{
    public static string Format(this string format, object arg, params object[] additionalArgs)
    {
        if (additionalArgs == null || additionalArgs.Length == 0)
        {
            return string.Format(format, arg);
        }
        else
        {
            return string.Format(format, new object[] { arg }.Concat(additionalArgs).ToArray());
        }
    }
}

Leave a comment

Please note that we won't show your email to others, or use it for sending unwanted emails. We will only use it to render your Gravatar image and to validate you as a real person.