This is inefficient:
if (dict.ContainsKey(key))
value = dict[key];
✅ Correct Way
if (dict.TryGetValue(key, out var value))
{
// use value
}
Why
-
Avoids double hash lookup
-
Faster under high frequency usage
Daily micro-tips for C#, SQL, performance, and scalable backend engineering.
This is inefficient:
if (dict.ContainsKey(key))
value = dict[key];
✅ Correct Way
if (dict.TryGetValue(key, out var value))
{
// use value
}
Why
Avoids double hash lookup
Faster under high frequency usage