Simplified Dictionary Lookups


When accessing values from a dictionary, it is common to check if the key exists before accessing the value to avoid exceptions when the key does not exist in the dictionary. This can be done using the ContainsKey method followed by an index identifier.

At first glance, this seems like a good practice. However, by turning on the code analysis rule CA1854 on our codebase, we learned that this isn't the case.

To fix this violation, the rule suggests using the TryGetValue method instead. This method avoids redundant lookups while accessing values from a dictionary. And, I also find that using TryGetValue makes the code easier to read.

After this change, which can be auto-fixed, the JetBrains tooling went a step further to simplify this code. The CanSimplifyDictionaryTryGetValueWithGetValueOrDefault code inspection, refactors this code block into a one-liner using the GetValueOrDefault method (also with an auto-fix). This is only applicable when a default value is used for values that do not exist in the dictionary.

As a result, we can make our codebases more efficient and concise.

Support me

I appreciate it if you would support me if have you enjoyed this post and found it useful, thank you in advance.

Buy Me a Coffee at PayPal logo

Join My Newsletter (WIP)

Join my weekly newsletter to receive my latest blog posts and bits, directly in your inbox.

Share this bit on

Twitter LinkedIn