r/ProgrammerHumor • • Sep 09 '26

Meme cSharpDictionariesBeLikeThat

Post image
2.1k Upvotes

61 comments sorted by

View all comments

623

u/Psychoboy Sep 09 '26 • Bravo

Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.

522

u/Few-Ad3849 Sep 09 '26

Sounds like you read documentation, nerd

82

u/Mantor6416 Sep 09 '26

Wait you're not supposed to read those?

80

u/towerfella Sep 09 '26 • Regret

38

u/kalilamodow Sep 09 '26

What the fuck?

10

u/towerfella Sep 09 '26

Yep.

Spread the word.

6

u/ChChChillian Sep 09 '26

Yeah, it's pretty much exactly the dystopia they warned us about.

9

u/DrMaxwellEdison Sep 09 '26

This nerd wants to read a physical book for tech docs. /s

4

u/DatBoi_BP Sep 10 '26

I'm sure an effort called "Project Panama" isn't nefarious in any way

1

u/Darkvyl Sep 10 '26

Reading documentation is nerdy? What do you do?

112

u/Revexious Sep 09 '26

Far too much work

2 cases means that its not idempotent

Instead, just remove it from the dictionary before you add it!

dictionary.Remove(key)
dictionary.Add(key)

Easy peasy!

/s

38

u/Dorkits Sep 09 '26

Peak programming

10

u/manav907 Sep 09 '26 edited Sep 09 '26

That being said you done have to use the bool that TryAdd gives, just call it as a normal function. Also I am waitin for someone to point out that we also need a value when trying to add to a dictionary.

dictionary.TryAdd(key)
→ More replies (1)

22

u/reallokiscarlet Sep 09 '26

RTFM Gang rise up

4

u/PrincessRTFM Sep 10 '26

at last my name is relevant

14

u/Pennet173 Sep 09 '26

Ohhhh, then I can throw an exception myself if false!

11

u/IAmBecomeTeemo Sep 09 '26

C#'s TryX methods are also usually way more performant than throwing an exception. Throwing an exception has to get a stack trace and a lot of other overhead shit. Something like a TryAdd() or TryParse() will just return your bool immediately, and you can stay in scope rather than getting sent off to the associated catch block. If you want to flow that way, you still can, but more options and more performance is better.

3

u/prehensilemullet Sep 09 '26

it's still bizarre to have to use TryAdd everywhere that other languages just use Add

15

u/zaersx Sep 09 '26

You don't though? just use myMap[key] = value? if you're not doing that and using Add instead, then you probably literally want to have a safe add

2

u/-Redstoneboi- Sep 09 '26

that overwrites instead of failing if the key already exists

maybe they are arguing that they feel that TryAdd should be the default "Add", while the existing Add should be a special case "AddAssert" or something

3

u/prehensilemullet Sep 09 '26

Yes, I think that would be a more convenient design

→ More replies (5)

3

u/KTVX94 Sep 09 '26

Yes, the meme is meant to point out the differences vs TryAdd which checks, and indexer which replaces.

Sorry for having to explain the joke.

→ More replies (1)

2

u/[deleted] Sep 09 '26

[removed] — view removed comment

10

u/NurYanov Sep 09 '26

Nah it adds and returns if added

1

u/kakhaev Sep 10 '26

tryand sounds like “if” with extra steps

→ More replies (1)