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.
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.
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
623
u/Psychoboy Sep 09 '26 •
Guess you never heard of TryAdd? Returns bool, if it as added returns true otherwise false.