@Konrad what surprises me is that in my tests, I'm passing the list into a method that accepts IEnumerable<T>
, so the runtime can't optimize it by calling the Count() extension method for IList<T>
.
I can only assume that the Count() extension method for IEnumerable is doing something like this:
public static int Count<T>(this IEnumerable<T> list)
{
if (list is IList<T>) return ((IList<T>)list).Count;
int i = 0;
foreach (var t in list) i++;
return i;
}
... in other words, a bit of runtime optimization for the special case of IList<T>
.
/EDIT @Konrad +1 mate - you're right about it more likely being on ICollection<T>
.