Count() is called on a list, array or other collection that knows its size

tx0/csharp:count-method-on-collectionstrictwarningperformance/algorithmicdepth fastperformance

What it finds

The LINQ method Count() checks for a known size at run time and falls back to counting. On a variable declared as a list, array, set or dictionary, the Count or Length property gives the size directly and says that no enumeration happens.

From the rule's source, count_method_on_collection.rs.

Example from the rule's tests
using System.Collections.Generic;using System.Linq; class Basket{    int Size(List<int> items, IEnumerable<int> stream) => items.Count() + stream.Count();tx0 reports: Count() on a List<int>; the Count property gives the size directly}
Highlighted lines are the ones tx0 reports, with its message under each; the other lines are left alone.

Turn it on, off or suppress it

The rule runs from the strict tier up. To turn it on in a lower tier, give it a level in tx0.toml:

tx0.toml
[rules]
"tx0/csharp:count-method-on-collection" = "warning"

To keep one finding, suppress it with a reason. tx0 suppress ID --reason "..." writes the comment on the line before the code:

// tx0: ignore[tx0/csharp:count-method-on-collection] reason="..."

tx0 explain tx0/csharp:count-method-on-collection shows the same in the terminal. Found a false positive? Report it.