06 Lazy Initialization of Dictionaries

Usually runtime dictionaries are initialized lazily. A request the the dictionary comes in and the appropriate value instance is created. This often leads to the following recurring code:

InstanceT inst;
if (!myDictionary.TryGetValue(key, out inst))
{
  inst = new InstanceT();
  myDictionary[key] = inst;
}
// code to use inst …

If only a default constructor is required to initialize the dictionary instances, you might consider to use the following hack:

public static ValueT GetOrCreate<KeyT, ValueT>(
  this Dictionary<KeyT, ValueT> dict, 
  KeyT key)
  where ValueT : new()
{
  ValueT r;
  if (!dict.TryGetValue(key, out r))
  {
    r = new ValueT();
    dict[key] = r;
  }
  return r;
}

which ultimately leads to much more concise code:

var inst = myDictionary.GetOrCreate(key);

I don’t recommend extending the extension method to accept a constructor lambda to catch the general case, because – as can be seen from the IL disassembly – the lambda would be created for for each access to the dictionary.