【发布时间】:2009-11-14 04:35:17
【问题描述】:
我正在寻找一个自定义字典的好名称,如果它不存在,它会使用委托自动初始化请求的键的值。索引器实现应该有助于理解它的作用:
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;
}
}
我的问题不在于代码,它运行良好,但我似乎无法为这个类找到合适的名称......我想过AutoInitDictionary,但它听起来不对,而且没有t 真正传达了“所有键都可以假定存在”的想法。
你会如何命名这样一个类?任何建议将不胜感激。
PS:如何使用它的示例:
var charFrequencies = new AutoInitDictionary<char, int>(key => 0);
foreach(char c in text)
charFrequencies[c]++;
【问题讨论】:
-
@Eric:好吧,我不这么认为......但无论如何感谢你的好笑;)
标签: c# .net dictionary naming-conventions