Hi,
I'm looking for a good name for a custom dictionary which automatically initializes the value for a requested key if it doesn't exist, using a delegate. The indexer implementation should help understand what it does :
public V this[K key]
{
get
{
V value;
if (!_dictionary.TryGetValue(key, out value))
{
value = _defaultValueGenerator(key);
_dictionary[key] = value;
}
return value;
}
set
{
_dictionary[key] = value;
}
}
My problem is not about the code, which works fine, but I can't seem to find a proper name for this class... I thought about AutoInitDictionary
, but it doesn't sound right, and doesn't really conveys the idea that "all keys can be assumed to exist".
How would you name such a class ? Any suggestion would be appreciated.
PS: an example of how it could be used :
var charFrequencies = new AutoInitDictionary<char, int>(key => 0);
foreach(char c in text)
charFrequencies[c]++;