LINQ .Any VS .Exists-有什么区别? [英] LINQ .Any VS .Exists - What's the difference?

查看:86
本文介绍了LINQ .Any VS .Exists-有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在集合上使用LINQ,以下几行代码有什么区别?

Using LINQ on collections, what is the difference between the following lines of code?

if(!coll.Any(i => i.Value))

if(!coll.Exists(i => i.Value))

更新1

当我反汇编.Exists时,似乎没有任何代码.

When I disassemble .Exists it looks like there is no code.

更新2

有人知道为什么那里没有代码吗?

Anyone know why there is no code there for this one?

推荐答案

查看文档

List.Exists (对象方法-MSDN)

List.Exists (Object method - MSDN)

确定List(T)是否包含与指定谓词定义的条件匹配的元素.

Determines whether the List(T) contains elements that match the conditions defined by the specified predicate.

自.NET 2.0开始存在,因此在LINQ之前存在.打算与谓词 delegate 一起使用,但是lambda表达式是向后兼容的.另外,只有List有这个(甚至没有IList)

This exists since .NET 2.0, so before LINQ. Meant to be used with the Predicate delegate, but lambda expressions are backward compatible. Also, just List has this (not even IList)

IEnumerable.Any (扩展方法-MSDN)

IEnumerable.Any (Extension method - MSDN)

确定序列中的任何元素是否满足条件.

Determines whether any element of a sequence satisfies a condition.

这是.NET 3.5中的新增功能,并使用Func(TSource,bool)作为参数,因此,该功能旨在与lambda表达式和LINQ一起使用.

This is new in .NET 3.5 and uses Func(TSource, bool) as argument, so this was intended to be used with lambda expressions and LINQ.

在行为上,这些是相同的.

In behaviour, these are identical.

这篇关于LINQ .Any VS .Exists-有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆