Enumerable.Count() is unnecessarily slow using a Covariant IEnumerable<T> - by Oren Novotny

Status : 

  Fixed<br /><br />
		This item has been fixed in the current or upcoming version of this product.<br /><br />
		A more detailed explanation for the resolution of this particular item may have been provided in the comments section.

Sign in
to vote
ID 454130 Comments
Status Closed Workarounds
Type Bug Repros 1
Opened 5/19/2009 3:01:48 PM
Access Restriction Public


Now that IEnumerable is covariant, some of the methods in Linq to Objects run slower than expected.  For example. Count().  Count checks to see if the input implements ICollection<TSource> and if so, shortcuts and returns .Count.  With a covariant IEnumerable though, it won't.  The easy workaround is for Enumerable.Count() to also check for the non-generic ICollection and if so return Count there.  As Count (and other similar methods, aren't type-specific, then it works.
Sign in to post a comment.
Posted by Microsoft on 6/4/2009 at 4:50 PM
Hi Oren,

It does indeed check both :)

David Sterling
Visual C# Compiler Team
Posted by Oren Novotny on 5/28/2009 at 8:50 PM

Long as it checks both ICollection<T> and ICollection as ICollection<T> does not derive from ICollection and some collections may not implement ICollection....wouldn't want to lose out on those ones either.

Posted by Microsoft on 5/28/2009 at 6:00 PM
Nice catch, Oren. I've changed the Count optimization to use ICollection instead of ICollection<T>.

Ed Maurer
Development Manager, C# & VB compilers.
Posted by Microsoft on 5/22/2009 at 3:50 AM
Thanks for your feedback.

We are escalating this issue to the appropriate group within the Visual Studio Product Team for triage and resolution.
These specialized experts will follow-up with your issue.
Posted by Microsoft on 5/21/2009 at 4:16 AM
Thank you for your feedback, We are currently reviewing the issue you have submitted. If this issue is urgent, please contact support directly(http://support.microsoft.com)