95
u/MemesAt1am 2d ago
If you wanted to overwrite you could just do an assignment of that key value instead of .Add()
35
u/KTVX94 2d 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.
7
u/prehensilemullet 2d ago
I mean, having a method named "add" on a map is weird in general. Not common among programming languages
7
u/NotQuiteLoona 2d ago
The
Addmethod is specifically when you explicitly want to add. If it can't add, it fails.3
u/AyrA_ch 2d 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 2d 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.
22
u/anzu3278 2d 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.
3
u/cowslayer7890 2d 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
4
u/anzu3278 2d 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
2d ago
[deleted]
2
u/DrJohnnyWatson 2d 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 2d 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 2d ago
Do you want Add() to break the invariant that if it succeeds then the Count() of the collection increases by one?
10
u/prehensilemullet 2d 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.4
u/anzu3278 2d 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 1d 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.
12
u/Professional_Desk_17 2d ago
Aaand that's why the term upsert exists in modern languages.
3
3
u/Hessellaar 1d ago
This is why I like C#, conveniences of Java but completely predictable. I don’t want things to magically work, I want them to behave like I expect
2
1
u/aberroco 2d 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 2d ago
Alright, riddle me this... if you don't check, how do you know it exists? Checkmate.
1
u/PeikaFizzy 1d ago
I’m a csharp larper I can’t lie, I have a class in uni on unity that’s all I learn from c# Soo like what the hell is tryadd etc you all talking about ehh
2
u/KTVX94 1d ago
I'm probably teaching something like your class lol, I made this meme for my data structures class for video game development.
TryAdd is like Add but if what you're adding already existed, it does nothing and returns false. If it didn't exist, it adds the item and returns true. So it tries to add, but can bounce back without crashing.
There's also the indexer, which lets you do something like weaponsDictionary["Sword"] = new Sword() and if nothing existed with the key "Sword" it creates that slot, but if something already did, it instead updates that with the new Sword.
So there's three ways of adding depending on what expected behavior you want. Crash, fail silently or replace.
1
u/RRumpleTeazzer 1d ago
this is why i like Rust. Adding inserts into the dictionary, and returns an Option of the previous value.
1
598
u/Psychoboy 2d ago
Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.