ReSharper 2024.1 Help

Code inspection: Dictionary item removal can be simplified with single 'Remove'

The Remove method in Dictionary<T, T> in C# does not throw an exception if the key is not found, it simply returns false. This means you do not need to use TryGetValue to check if the key exists in the dictionary before trying to remove it.

void RemoveValue(Dictionary<int, int> d, int toRemove) { if (d.TryGetValue(toRemove, out _)) { d.Remove(toRemove); } }
void RemoveValue(Dictionary<int, int> d, int toRemove) { d.Remove(toRemove, out _); }
Last modified: 08 April 2024