4 ms·
Don't .NET dictionaries throw when a requested key isn't found?
by scotth 7y ago
Don't .NET dictionaries throw when a requested key isn't found?
- alkonaut 7y agoYes, for the indexer scenario dict[key] that is an exceptional failure (equivalent to indexing list[index] wher index >= length). The method to get the value of a key only if the key is in the dictionary is TryGet... I think this design is the best one. It lets you have the program blow up quickly in all cases where the presence of the key is a requirement for the program to be in a valid state (which is common). Otherwise you'd be doing x = dict[key] if (x == null) throw("Unexpected this is. And unfortunate");
- ailideex 7y agoChecked access to c++ containers also throw ... https://en.cppreference.com/w/cpp/container/map/at https://en.cppreference.com/w/cpp/container/map/at
- SigmundA 7y agoYes and probably make the most sense as .Net has no "undefined" value. If the dictionary has a the key with a null value it returns null, but no key throws. With generic dictionaries (which everyone uses now days) you have TryGetValue which is a little weird since it uses a out parameter but pretty natural after c# 7 inline out params.
- 7777fps 7y agoI often think it's a shame there isn't a "GetOrDefault" for dictionaries. An extension like > dict<k,v>.GetOrDefault(k key,v valueIfNotPresent) Would be really helpful. It's easy enough to write your own extension to do this, but something built in would be the kind of nice to have method that has been creeping into .net.
- SigmundA 7y agoYes I agree, ImmutableDictionary has GetValueOrDefault, GetOrAdd is nice on ConcurrentDictionary and FirstOrDefault is nice in Linq.