Join Nostr
2026-11-21 13:37:42 UTC

codementor on Nostr: Learn: Never change a key while it is in a HashSet A HashSet or Dictionary files each ...

Learn: Never change a key while it is in a HashSet

A HashSet or Dictionary files each key by its hash code. If the key changes, its hash code changes, and the set looks in the wrong place.

```csharp
var p = new Point { X = 1 };
var set = new HashSet<Point> { p };
p.X = 2; // changes p's hash code
Console.WriteLine(set.Contains(p));
```

It prints False, even though p is in the set. Use keys that can't change, like strings, numbers or records without setters.

#tip · TIP-012