Updated: A Better Dictionary Initializer

Alex Henderson tactfully commented on my last post that he had already been there, done that, and got the tee shirt.  Based on his work, a refactored Hash.Init would go like this:

public static IDictionary Init(params Func<object, object>[] funcs)
{
  IDictionary values = new Hashtable(funcs.Length, StringComparer.OrdinalIgnoreCase);

  foreach (Func<object, object> func in funcs)
  {
    string key = func.Method.GetParameters()[0].Name;
    object value = func(null);
    values.Add(key, value);
  }

  return values;
}

That'll teach me for thinking I'm being innovative :)

posted @ Sunday, December 30, 2007 2:42 PM


Print

Comments on this entry:

# mucking about with hashes...

Left by Bitter Coder at 1/7/2008 1:50 PM

Your comment:



 (will not be displayed)


 
 
 
Please add 7 and 1 and type the answer here:
 

Live Comment Preview:

 
«July»
SunMonTueWedThuFriSat
293012345
6789101112
13141516171819
20212223242526
272829303112
3456789