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的顺序以减少列表的已分配内存的大小.现在它只会为要删除的项目分配一个列表.
| 归档时间: |
|
| 查看次数: |
10075 次 |
| 最近记录: |