Dictionary Extension Method - Append

I've been creating a few extension methods here and there recently and figured I'd share one of the ones I'm using in a lot of places. This one is an extension method named Append that you can use with a generic IDictionary implementor to do quick-and-dirty creations of the object. I've found this to be really useful when I'm writing tests that use the generic IDictionary class in my tests. The first block of code below will point out the extension method.

 

public static class DictionaryExtensions
{
  public static IDictionary Append(this IDictionary dictionary,
    TKey key, TValue value)
  {
    dictionary.Add(key, value);
    return dictionary;
  }
}

 

This second block demonstrates one potential use for it. In this case I was creating a generic Dictionary to store name/value pairs of data for an HTTP post. 

 

IDictionary prms = new Dictionary().Append("status", status);

 

Happy coding!

Brady Gaster
Hi! I'm a christian dad who lives near Seattle, where I work with the talented folks at Microsoft to create compelling demonstrations for conferences that instruct and inspire developers who want to party in the cloud.