A bug I had a lot was adding a value to a dictionary with a key using specific casing and using different casing later on to check if the key exists in the dictionary. Typically I try to avoid this problem by always changing the key to lowercase (or uppercase) before calling the ContainsKey
method, but of course this is not fool proof.
Turns out there is a better way when using strings as keys. You can pass a StringComparer
as a constructor argument when creating the dictionary:
Now we can try different casing and all will return true: