Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the minimum interface that has the Count property in .Net

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.

like image 747
Ignacio Soler Garcia Avatar asked May 21 '10 07:05

Ignacio Soler Garcia


3 Answers

ICollection adds the Count property.

As @Joren rightly points point, IEnumerable<T> has the extension method Count<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.

like image 133
David Neale Avatar answered Oct 13 '22 01:10

David Neale


ICollection adds the Count property.

like image 42
Dean Harding Avatar answered Oct 13 '22 00:10

Dean Harding


System.Collections.ICollection, and also System.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).

like image 25
Thorarin Avatar answered Oct 13 '22 01:10

Thorarin