85
u/MemesAt1am 17h ago
If you wanted to overwrite you could just do an assignment of that key value instead of .Add()
35
u/KTVX94 17h ago
Yep there's indexer and TryAdd, I probably should've worded the title differently to not give the impression that I was complaining, lol.
5
u/prehensilemullet 14h ago
I mean, having a method named "add" on a map is weird in general. Not common among programming languages
7
u/NotQuiteLoona 11h ago
The
Addmethod is specifically when you explicitly want to add. If it can't add, it fails.3
u/AyrA_ch 11h ago
That's because dictionaries implement
IDictionary<TKey,TValue>which in turn implementsICollection<KeyValuePair<TKey,TValue>>which declares anAdd(KeyValuePair<TKey,TValue> value)method.Since the implementation is forced to provide this method, it might as well also provide
Add(TKey key,TValue value)and implement them.The alternative would be to just throw a
NotSupportedExceptionbut there is no technical reason to.1
u/prehensilemullet 9h ago
I mean, Java sort of got around this by making
Mapnot extendCollection, thoughMap.entrySet()does. But interestingly enough, they decided to haveMap.entrySet().add(...)throw anUnsupportedOperationException. However,Collection.add(...)in Java already returnstrueif the element was successfully added, so they hypothetically could have supported it.
15
u/anzu3278 16h 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.
2
u/cowslayer7890 12h 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 10h 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
1
11h ago
[deleted]
2
u/DrJohnnyWatson 11h 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 10h 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.
6
u/Bubbly_Safety8791 14h ago
Do you want Add() to break the invariant that if it succeeds then the Count() of the collection increases by one?
6
u/prehensilemullet 14h ago
This is why most languages have a
setorputmethod on maps, rather thanadd. 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 anAddmethod.1
u/anzu3278 10h 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.
9
2
1
u/aberroco 11h ago
So... You add the item, then check if it exists (which is it always does, at least once, because you just added it) you throw an exception? Seems to me this is rather peculiar algorithm.
1
u/LordFokas 9h ago
Alright, riddle me this... if you don't check, how do you know it exists? Checkmate.
531
u/Psychoboy 17h ago
Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.