We recently had an issue where we called a dictionary to retrieve a value, expecting the key to be present. It wasn’t leading to a process breaking.
Does ReSharper have the functionality to check for this like I’ve heard it can for null objects?
Here is an example to support what I am talking about:
Dictionary<String, Entity> allEntities =
new Dictionary<String, Entity>(SringComparer.OrdinalIgnoreCase);
allEntities.AddMany(db.GetAllEntities());
Entity thisEntity = allEntities[entityID];
// <-- error here as EntityID isn't in all entities...
I would like ReSharper to be able to say I haven’t checked the dictionary like so:
if (allEntities.ContainsKey(entityID))
...
As an FYI, I don’t have ReSharper, but this would be one more thing to add in to the business case to get it for all developers.
I have currently installed ReSharper 6, and at least with my settings, it does not warn me to check a
DictionarywithContainsKeybefore accessing its value.