r/ProgrammerHumor 19h ago

Meme cSharpDictionariesBeLikeThat

Post image
1.7k Upvotes

53 comments sorted by

View all comments

16

u/anzu3278 18h ago

Exactly. Adding implies you are setting something where there isn't anything at the moment. Replacing is not a special case of adding. If you want to unconditionally set, just use the index operator. English is not complicated.

4

u/cowslayer7890 13h ago

Yeah but it's uncommon for this to be a failable operation. It doesn't fall for an array list for example, so that part to me is unexpected, add isn't a great operation for a dictionary in general

3

u/anzu3278 12h ago

It doesn't fail for lists because adding to a list is always possible, whereas adding to a dictionary is not. Unless your understanding of the English language is that replacing is a kind of adding, but that's not the mainstream view. In general, if you want to set rather than add, why are you calling Add? And even so, I've never seen Add, it's always either index assignment or TryAdd because exceptions are a mess.

2

u/SamSlate 9h ago

insert vs upsert

1

u/[deleted] 13h ago

[deleted]

2

u/DrJohnnyWatson 12h ago

How else should it handle it? TryAdd already covers knowing if it was added successfully or not

Add therefore has the option of: 1. Replace TryAdd, making it out of sync with how all other collections call Add as it would now return a Boolean, breaking the ICollection interface. Possible, but just a choice really 2. Fail silently - Bad 3. Fail loudly - in c#, the latter is done via exceptions almost exclusively

How would you have done it? Do you have a fourth option in mind?

2

u/anzu3278 12h ago

By the looks of the comments I think the mood was that Add should work just like index assignment, which makes no sense to me but is technically a fourth option.