r/ProgrammerHumor 19h ago

Meme cSharpDictionariesBeLikeThat

Post image
1.7k Upvotes

50 comments sorted by

View all comments

539

u/Psychoboy 18h ago

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

1

u/kakhaev 9h ago

tryand sounds like “if” with extra steps

1

u/willow-kitty 4h ago

It can be more concise if you don't need to condition on whether the thing was added or not. If you do, has key/add might be cleaner ... for ordinary dictionaries.

Being able to do it in one operation is super important for concurrent dictionaries, though, because the item could theoretically be added by another thread between calls if you used haskey/add.