A set or dictionary is searched with Contains or ContainsKey and then added to, where one call would do both

tx0/csharp:check-then-addstrictwarningperformance/algorithmicdepth fastperformance

What it finds

if (!seen.Contains(x)) seen.Add(x); searches the set twice; seen.Add(x) already returns whether it added. For dictionaries, TryAdd adds only when the key is missing.

From the rule's source, check_then_add.rs.

Example from the rule's tests
using System.Collections.Generic; class Seen{    void Mark(HashSet<string> seen, string name)    {        if (!seen.Contains(name))tx0 reports: the collection is searched twice; the result of Add does both        {            seen.Add(name);        }    }}
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:check-then-add" = "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:check-then-add] reason="..."

tx0 explain tx0/csharp:check-then-add shows the same in the terminal. Found a false positive? Report it.