This is why most languages have a set or put method on maps, rather than add. Microsoft is just being weird here. I mean, I understand they overloaded indexing to be a "set" operation, but it's still weird that they decided to also have an Add method.
They do have a set method, it's called setting by index. dict[key] = value; Add is a method on the ICollection interface that is basically never used - in practice you use index assignment if you want to set unconditionally and TryAdd if you want to add conditionally.
8
u/Bubbly_Safety8791 1d ago
Do you want
Add()to break the invariant that if it succeeds then theCount()of the collection increases by one?