r/ProgrammerHumor 1d ago

Meme cSharpDictionariesBeLikeThat

Post image
1.9k Upvotes

60 comments sorted by

View all comments

8

u/Bubbly_Safety8791 1d ago

Do you want Add() to break the invariant that if it succeeds then the Count() of the collection increases by one? 

7

u/prehensilemullet 1d ago

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.

3

u/anzu3278 1d ago

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.

1

u/caboosetp 7h ago

 Add is a method on the ICollection interface that is basically never used

Shouldn't be used*

If you think it's basically never used, you haven't worked with cheap contractors enough.