.Net中具有Count属性的最小接口是什么
我需要更改一个方法,该方法有一个参数接受一系列对象。我需要找到具有 Count 属性的最低接口(在继承树中)。到目前为止,我一直在使用 IEnumerable,但由于它还没有 Count,我需要将其更改为可能的更广泛的接口,以便该方法可以处理最大数量的系列类型(集合、列表、数组等)。
提前致谢。
I need to change a method that has one parameter that takes a serie of objects. I need to find the lowest Interface (in inheritance tree) that has the Count property. Until now I was using the IEnumerable but as this has not Count I need to change it to the wider interface possible so the method can work with the biggest number of types of series (collections, lists, arrays, etc).
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
ICollection
添加Count
属性。正如 @Joren 正确指出的那样,如果您乐意使集合通用,则
IEnumerable
具有扩展方法Count()
。然而,正如 @Joel Coehoorn 指出的那样,不建议使用它,因为它会强制序列迭代。ICollection
adds theCount
property.As @Joren rightly points point,
IEnumerable<T>
has the extension methodCount<T>()
if you're happy making your collection generic. However, as @Joel Coehoorn has pointed out, it is inadvisable to use this as it forces an iteration of the sequence.ICollection 添加 Count 属性。
ICollection adds the Count property.
System.Collections.ICollection
,以及System.Collections.Generic.ICollection;
。这两个接口彼此没有关系,但都继承自IEnumerable,因此它们处于同一级别。IEnumerable 显然没有 Count 属性(计数不一定是预先确定的)。
System.Collections.ICollection
, and alsoSystem.Collections.Generic.ICollection<T>
. These two interfaces have no relation to eachother, but both inherit from IEnumerable, so they're at the same level.IEnumerable obviously does not have a Count property (the count isn't necessarily predetermined).