02 string.Format() extension method.

A nice use for an extension method is string.Format(), so instead of

var formatted = string.Format("My Format string {0}, {1}", firstParameter, secondParameter);

you may simply use

var formatted = "My Format string {0}, {1}"
  .Format(firstParameter, secondParameter); 

An example implementation may look like the following:

public static class StringExtensions
{
  public static string Format(this string format, params object[] objects)
  {
    if (objects.Length == 0)
      return format;
 
    try
    {
      return string.Format(format, objects);
    }
    catch (Exception e)
    {
      return e.Message;
    }
  }
}

The above code catches all exceptions that happen in the formatting process. This is useful in scenarios, where the format string may be constructed and so might not match the arguments provided.