Hacker News new | ask | show | jobs
by chrisb 5877 days ago
The .Count() method in LINQ to objects checks to see if the IEnumerable implements ICollection, and if so then just calls the .Count property.

Of course, calling the .Count property yourself is more efficient, but not by orders of magnitude.

1 comments

Thank you for this, this was apparently fixed in .NET 4.0 which I was unaware of.

I think there is still some validity in my post in confusing whether you are using native accessing or routing through an additional tool/library, but the fix definitely elimates the performance hit I was discussing, so much of my post was in error.

edit: Also 3.5 it seems, I need to be more up-to-date with my concerns.

Everyone get's that wrong, including Microsoft's documentation ;). It's a good optimization though, in spite of the LSP violation.