Skip to content
This repository has been archived by the owner on Jan 23, 2023. It is now read-only.
/ corefx Public archive

Use Count in Enumerable.Any if available #40377

Merged
merged 2 commits into from
Aug 17, 2019

Commits on Aug 17, 2019

  1. Use Count in Enumerable.Any if available

    We've been hesitant to make this change in the past, as it adds several interface checks.  However, wide-spread "wisdom" is that `Any()` is as fast or faster than `Count() > 0`, and there are even FxCop rules/analyzers that warn about using the latter instead of the former, but in its current form that can frequently be incorrect: if the source does implement `ICollection<T>`, generally its `Count` is O(1) and allocation-free, whereas `Any()` will almost always end up allocating an enumerator.  On balance, it seems better to just have `Any()` map closely to `Count()` so that their performance can be reasoned about in parallel.
    stephentoub committed Aug 17, 2019
    Configuration menu
    Copy the full SHA
    bdf6a79 View commit details
    Browse the repository at this point in the history
  2. Configuration menu
    Copy the full SHA
    f47fce3 View commit details
    Browse the repository at this point in the history