如何使用谓词在 C# 中创建扩展方法
我正在尝试创建一个名为 RemoveWhere 的扩展方法,该方法根据谓词从 List 集合中删除项目。例如
var result = products.RemoveWhere(p => p.ID == 5);
我使用 Microsoft 的Where 扩展方法签名作为起点。到目前为止,这是我所得到的:
public static List<T> RemoveWhere<T>(this List<T> source, Func<T, List<T>> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source", "The sequence is null and contains no elements.");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate", "The predicate function is null and cannot be executed.");
}
// how to use predicate here???
}
我不知道如何使用谓词。有人可以帮我完成这个吗?谢谢你!
I'm trying to create an extension method called RemoveWhere that removes an item from a List collection based on a predicate. For example
var result = products.RemoveWhere(p => p.ID == 5);
I'm using Microsoft's Where extension method signature as a starting point. Here's what I have so far:
public static List<T> RemoveWhere<T>(this List<T> source, Func<T, List<T>> predicate)
{
if (source == null)
{
throw new ArgumentNullException("source", "The sequence is null and contains no elements.");
}
if (predicate == null)
{
throw new ArgumentNullException("predicate", "The predicate function is null and cannot be executed.");
}
// how to use predicate here???
}
I don't know how to use the predicate. Can someone help me finish this? Thank you!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
Predicate 参数应该是:
Func
EDIT: 正如其他人所指出的,此方法要么命名错误,要么已经存在于 List 中。我的猜测只是您试图了解方法本身如何使用传入的委托。为此,您可以查看我的示例。如果这不是您的意图,我将删除这个答案,因为代码确实毫无意义。
The Predicate parameter should be:
Func<T,bool>
EDIT: As others have pointed out, this method is either misnamed, or it already exists on List. My guess is just that you're trying to understand how a passed in delegate is used by the method itself. For that you can look at my sample. If that is not your intent, I'll delete this answer as the code really is kind of pointless.
列表中已经有一个方法可以尝试这样做。 Predicate 应该是 Predicate 那么你可以使用 source.RemoveAll(predicate)
There is already a method in list that does that try. Predicate should be a Predicate then you can use source.RemoveAll(predicate)
正如其他人指出的那样,
List.RemoveAll
将执行您想要的操作。但是,如果这是一次学习体验,或者您想要对任何IList
(没有RemoveAll
)进行操作,则这应该与执行相同的操作RemoveAll
而是作为扩展方法。As others have pointed out,
List<T>.RemoveAll
will do what you want. If however, this is a learning experience or you want to operate on anyIList<T>
(which doesn't haveRemoveAll
) this should do the same asRemoveAll
but as an extension method.正如 boca 观察到的,
List
已经有一个方法可以做到这一点。不过,一个更大的问题是,这确实不是您应该创建新扩展方法的场景。已经有一个采用谓词的扩展方法:Where
。当然,这样做:
比使用
RemoveAll
代码多一点:但是:
list
实际上可以是任何IEnumerable
,不仅仅是一个List
,ToList ()
并枚举结果
。我真的很难想象我想要为采用谓词的
IEnumerable
编写扩展方法的情况。如果您不使用Where()
,那么您节省的费用就很少。As boca observed,
List<T>
already has a method to do this. A somewhat larger issue, though, is that this really isn't a scenario where you should be creating a new extension method. There's already an extension method that takes a predicate:Where
.Granted, doing this:
is a little more code than using
RemoveAll
:But:
list
can actually be anyIEnumerable<T>
, not just aList<T>
,Where
method is a commonly used, well-documented extension method that any reasonably-skilled C# programmer can be expected to recognize on sightToList()
and enumerate overresult
.It's really hard for me to envision circumstances where I'd want to write an extension method for
IEnumerable<T>
that takes a predicate. You're saving very little by making it possible for you to not useWhere()
.