扩展方法Dictionary <TKey,TValue> .RemoveAll?可能吗?

Rob*_*ett 7 generics extension-methods c#-3.0

我一直在尝试编写一个扩展方法来模仿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;
}
Run Code Online (Sandbox Code Playgroud)

有什么指针吗?这是一个完全天真的实现吗?

Jar*_*Par 16

您的代码将无法正常工作,因为您按值传递Dictionary类.这意味着调用函数将无法看到最终赋值(dict = temp).在C#中通过ref或out传递扩展方法目标是不合法的(在VB中,做ByRef是合法的).

相反,您需要修改字典内联.请尝试以下方法

public static void RemoveAll<TKey,TValue>(this Dictionary<TKey,TValue> dict, 
                                     Func<KeyValuePair<TKey,TValue>,bool> condition)
{
    foreach ( var cur in dict.Where(condition).ToList() ) {
      dict.Remove(cur.Key);
    }
}
Run Code Online (Sandbox Code Playgroud)

编辑

交换Where和ToList的顺序以减少列表的已分配内存的大小.现在它只会为要删除的项目分配一个列表.

  • @Lucero,他没有循环,他正在循环一个列表,这是一个投影.@Rob Stevenson-Leggett,我很惊讶您接受了这个答案,因为它改变了您所需功能的签名(旨在复制List.RemoveAll).您可以用Predicate <>匹配替换Func <>条件,然后.Where(x => match(x)) (3认同)