列表任何还是计数?
当我想对列表执行某些操作时,我首先检查它是否不为 null
或不包含任何元素(不要破坏 foreach
),我通常使用 list .Any()
但最好的选择是什么 - 使用 list.Count > 0
,还是使用 list.Any()
?
When I want to do something with a list I first check it if is not null
or contains no elements (not to blow a foreach
) and I usually use list.Any()
but what is the best option - to use list.Count > 0
, or to use list.Any()
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
List
,请使用Count
,因为它知道其大小。Array
使用Length
IEnumerable
,我会使用.Any()
而不是.Count()
因为它会更快,因为它在检查一项后停止。另请查看这个问题:哪种方法性能更好:.Any() vs .Count() > 0?
Count
if you're using aList
, since it knows its size.Length
for anArray
IEnumerable
I would use.Any()
over.Count()
as it will be faster since it stops after checking one item.Also check out this question: Which method performs better: .Any() vs .Count() > 0?
我使用
list.Count > 0
只是因为它不依赖于 LINQ 方法,因此适用于 C# 2.0。我个人像躲避瘟疫一样避开 LINQ(因为它的速度慢),而且无论如何也没有理由在这里使用扩展方法。
但是,更好的解决方案可能是制作您自己的
Any
版本,该版本将接受null
引用,如果是包含元素的集合则返回 true。这将为您节省空检查。I use
list.Count > 0
just because it doesn't depend on the LINQ methods and so works on C# 2.0.I personally avoid LINQ like the plague (because of its slow speed), and there's no reason to use extension methods here at all anyway.
However, a better solution would probably be to make your own version of
Any
that would take in anull
reference, and return true if it's a collection with elements. That would save you the null check..Any()
通常比.Count() > 更好用0 。这样做的原因是,如果您要迭代的项目不是 ICollection,那么它将必须迭代整个列表才能获取计数。
但是,如果项目是一个
ICollection
(List
就是这样),那么使用Count()< 的速度同样快,或者在某些情况下更快。 /code> (
Any()
迭代一次,无论 MS .Net 中的底层类型如何,但当底层项是时,Mono 会尝试将其优化为
)Count > 0
>ICollectionReflector 是一个很棒的工具,.Net 源代码和 < a href="https://github.com/mono/mono/blob/master/mcs/class/System.Core/System.Linq/Enumerable.cs" rel="nofollow noreferrer">单声道源代码让您可以看到事情是如何实现的。
.Any()
is generally better to use than.Count() > 0
. The reason for this is that if the items you are iterating over is not anICollection
then it will have to iterate the whole list to get the count.But if the items is an
ICollection
(which aList<T>
is) then it is just as fast or in some cases faster to useCount()
(Any()
iterates once regardless of underlying type in MS .Net but Mono tries to optimize this toCount > 0
when the underlying items is anICollection
)A great tool is Reflector, the .Net source code and the Mono source code which allows you to see how things are implemented.
如果您使用实体框架并且有一个包含许多记录的巨大表,Any() 会快得多。我记得有一次我想检查一张表是否为空并且它有数百万行。 Count() > 花费了 20-30 秒0 完成。 Any() 是即时的。
If you are using the Entity Framework and have a huge table with many records Any() will be much faster. I remember one time I wanted to check to see if a table was empty and it had millions of rows. It took 20-30 seconds for Count() > 0 to complete. It was instant with Any().
Any() 可以提高性能,因为它可能不必迭代集合来获取事物的数量。只要击中其中一个就可以了。或者,对于 LINQ-to-Entities,生成的 SQL 将是 IF EXISTS(...) 而不是 SELECT COUNT ... 甚至 SELECT * ...。
Any() can be a performance enhancement because it may not have to iterate the collection to get the number of things. It just has to hit one of them. Or, for, say, LINQ-to-Entities, the generated SQL will be IF EXISTS(...) rather than SELECT COUNT ... or even SELECT * ....