来自ICollection< T>的属性Count.仍在向后兼容? [英] The property Count from ICollection<T> is still around for backward compatibility?
问题描述
以前的上下文
要了解我的问题,请首先查看问题为什么.Count没有括号就可以工作?"
To understand what I'm questioning, see first the question "Why does .Count work without parentheses?"
在讨论该问题时,为什么在实现ICollection<T>
的类中存在Count
(属性)和Count()
(方法).该问题是令人满意的答案.
In that question is discussed, Why exists Count
(property) and Count()
(method) in classes that implements ICollection<T>
. that question was answered satisfactorily.
但是在评论的答案下面, finoutlook
提出了另一个问题:
But below the answer in a comment, another question was raised by finoutlook
:
问题
为了向后兼容,属性Count
是否仍然存在(在LINq技术的.NET更高版本中)?
The property Count
is still around (in later .Net versions to LINq technology) for backward compatibility?
推荐答案
不,它是在周围,因此一个集合可以直接返回有多少个元素,而不必对其进行迭代.
No, it's around so that a collection can return how many elements there are directly, without having to iterate over it.
Enumerable.Count()
进行了优化,如果集合实现了ICollection<T>
,它将使用该属性,而不是一次遍历集合中的一个元素,否则您将必须这样做.
Enumerable.Count()
has an optimization such that if the collection implements ICollection<T>
, it will use the property instead of iterating over the collection one element at a time, which is what you'd have to do otherwise.
对我来说,这似乎是明显有用的财产.即使今天就考虑到LINQ来创建框架,我相信仍然会有一些非常相似的东西. (诚然,我可以想到我希望对集合接口进行的各种更改,但这是另一回事.)
It feels like a clearly useful property to me. Even if the framework were created today with LINQ in mind, I'm sure there would still be something very similar. (Admittedly I can think of various changes I'd like to see to the collection interfaces, but that's a different matter.)
这篇关于来自ICollection< T>的属性Count.仍在向后兼容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!