【发布时间】:2009-03-17 14:16:52
【问题描述】:
我一直在尝试编写一个扩展方法来模仿 List.RemoveAll(Predicate)。
到目前为止,我得到了这个:
public static void RemoveAll<TKey,TValue>(this Dictionary<TKey,TValue> dict,
Predicate<KeyValuePair<TKey,TValue>> condition)
{
Dictionary<TKey,TValue> temp = new Dictionary<TKey,TValue>();
foreach (var item in dict)
{
if (!condition.Invoke(item))
temp.Add(item.Key, item.Value);
}
dict = temp;
}
有什么建议吗?这是一个完全幼稚的实现吗?
【问题讨论】:
-
您不想通过仅通过键而不是 KeyValuePair 匹配谓词来从字典中删除对吗?
标签: generics c#-3.0 extension-methods