I am a total neat freak when it comes to code (my desk, though, is another matter). Whenever I find myself writing the same or very similar code within an application, I immediately want to factor out to a common, shared method. When something is repeated many times (say, 10-15 or more), my desire is to move the differences into some statically intialized data that can be used to provide the parameters necessary to drive the factored out code. In the case of a .NET application you can use the generic Dictionary<TKey, TValue> as a lookup table. The problem is that, unlike List<T>, generic Dictionary has no constructor which allows you to initialize it with data. Necessity being the mother of invention, I created the InitializeableDictionary<TKey, TValue> class.
I am actually a little surprised this is not part of the standard Dictionary class. They would just need to add a few constructors like with List, but, alas, this is not so. However, I understand there are all kinds of reasons for why features do or do not make it in to a product (heck, I used to work on the Visual Studio team as part of Team System, so I ought to understand). So I created InitializableDictionary a couple years ago to fill this gap and have ended up copying it over to just about every .NET project I have worked on since then. So, I thought I would share it here for anyone that could benefit from it. It is very simple. It just adds a constructor that takes in an array of KeyValuePair. The code is here:
using System.Collections.Generic;
Â
namespace ITDevWorks.Internal.Website.Classes
{
    /// <summary>
    /// Summary description for InitializableDictionary
    /// </summary>
    public class InitializableDictionary<TKey, TValue>
        : Dictionary<TKey, TValue>
    {
        public InitializableDictionary(KeyValuePair<TKey, TValue>[] values)
            : this(null, values)
        {
        }
Â
        public InitializableDictionary(IEqualityComparer<TKey> comparer,
                                       KeyValuePair<TKey, TValue>[] values)
            : base(comparer)
        {
            if (null != values && 0 < values.Length)
            {
                foreach (KeyValuePair<TKey, TValue> pair in values)
                {
                    this.Add(pair.Key, pair.Value);
                }
            }
        }
    }
}
Â
The constructor with the IEqualityComparer<> is for cases when I need a dictionary with a case insensitive key. Note that you only need to use the InitializableDictionary constructor, there is no need to make your actual object of that class. Here is some example code that uses it (note that it does get a little wordy, but once you get the first line out of the way, up to the open curly brace, then the data lines are not so bad):
private static readonly Dictionary<string, string> urlMap =
    new InitializableDictionary<string, string>(
            StringComparer.InvariantCultureIgnoreCase,
            new KeyValuePair<string, string>[] {
    new KeyValuePair<string, IntraSiteUrlConstants>("key1", "value1"),
    new KeyValuePair<string, IntraSiteUrlConstants>("key2", "value2"),
});
Â
Of course, this example just uses a simple string to string mapping, but in a real application the key or the value could be of just about any type.
I hope you find this useful. If you do, let me know by posting a comment below.
Thanks,
Dave

{ 1 comment… read it below or add one }
Other variant is possible also