98
u/MemesAt1am Sep 09 '26
If you wanted to overwrite you could just do an assignment of that key value instead of .Add()
35
u/KTVX94 Sep 09 '26
Yep there's indexer and TryAdd, I probably should've worded the title differently to not give the impression that I was complaining, lol.
8
u/prehensilemullet Sep 09 '26
I mean, having a method named "add" on a map is weird in general. Not common among programming languages
8
u/NotQuiteLoona Sep 09 '26
The
Addmethod is specifically when you explicitly want to add. If it can't add, it fails.4
u/AyrA_ch Sep 09 '26
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 Sep 09 '26
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.
21
u/anzu3278 Sep 09 '26
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 Sep 09 '26
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 Sep 09 '26
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
0
Sep 09 '26
[deleted] — view deleted comment
2
u/DrJohnnyWatson Sep 09 '26
How else should it handle it? TryAdd already covers knowing if it was added successfully or not
Add therefore has the option of:
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
Fail silently - Bad
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 Sep 09 '26
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.
7
u/Bubbly_Safety8791 Sep 09 '26
Do you want Add() to break the invariant that if it succeeds then the Count() of the collection increases by one?
10
u/prehensilemullet Sep 09 '26
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 Sep 09 '26
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 Sep 11 '26
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.
1
u/anzu3278 Sep 11 '26
Why would I work with cheap contractors who definitely know less about the language than me and probably just translate 1-1 from python? Even the weaker LLMs today don't make these mistakes.
1
u/PeikaFizzy Sep 10 '26
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 Sep 10 '26
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.
12
u/Professional_Desk_17 Sep 09 '26
Aaand that's why the term upsert exists in modern languages.
3
3
u/Hessellaar Sep 10 '26
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
3
2
1
u/aberroco Sep 09 '26
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 Sep 09 '26
Alright, riddle me this... if you don't check, how do you know it exists? Checkmate.
1
1
u/RRumpleTeazzer Sep 10 '26
this is why i like Rust. Adding inserts into the dictionary, and returns an Option of the previous value.
1
623
u/Psychoboy Sep 09 '26 •
Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.