Count() is called on a list, array or other collection that knows its size
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.
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} 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:
[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.