Timeline for What's the use of .Any() in a C# List<>?
Current License: CC BY-SA 3.0
9 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Sep 5, 2015 at 17:31 | history | edited | sstan | CC BY-SA 3.0 | added 982 characters in body |
| Sep 5, 2015 at 17:24 | comment | added | sstan | @tahatmat: Absolutely right. I had not noticed that in the implementation. Thanks for pointing that out. | |
| Sep 5, 2015 at 17:20 | comment | added | Gusdor | Linq extensions are optimised for known interfaces - IList , ICollection. Performance discussion here is totally redundant, even if that is an implementation detail | |
| Sep 5, 2015 at 7:38 | comment | added | Thorkil Holm-Jacobsen | Count() has the same complexity as Count for ICollections, since the extension method then uses the property instead of iterating. | |
| Sep 4, 2015 at 19:10 | comment | added | Esben Skov Pedersen | @sstan yes unless if the ienumerable can be cast to a collention then it can be optimized and it won't matter. | |
| Sep 4, 2015 at 18:34 | comment | added | sstan | With a List<T> the performance difference will be negligible. So just use what you prefer. But for other types of IEnumerables, you only get to pick between Count() (the method), and Any(), and in that case, Any() will always be the clear winner for your use case and should be preferred. For what it's worth, I think Any() is quite readable and clear. | |
| Sep 4, 2015 at 18:33 | review | First posts | |||
| Sep 4, 2015 at 21:11 | |||||
| Sep 4, 2015 at 18:29 | comment | added | Gil Sand | Fair enough. What about the use of the .Count property then? wouldn't that make the read significantly faster, maybe as fast or even faster than calling .Any() ? | |
| Sep 4, 2015 at 18:28 | history | answered | sstan | CC BY-SA 3.0 |