C# TryGetValue (Get Value From Dictionary)

These C# examples show the TryGetValue method on Dictionary. They get values from dictionaries.
TryGetValue. This method optimizes Dictionary usage. It gets a value (at a key) from a Dictionary. And it eliminates unneeded lookups, making programs better.
Some notes. With TryGetValue, we can combine the "try" part of seeing if a key exists, and the "get" part of getting the existing value. This saves 1 lookup.
TryGetValue, example. We can rewrite programs that use ContainsKey with TryGetValue. By combining two operations (try and get) into 1, we can optimize and simplify programs.

Important: Every lookup has to compute the hash code, which has a performance penalty. The TryGetValue method can help here.

Value: Look carefully at "value" in this program. It is accessed by TryGetValue, and we do not need to get it again when we use it.

C# program that uses TryGetValue using System; using System.Collections.Generic; class Program { static void Main() { var counts = new Dictionary<string, int>(); counts.Add("key", 0); // Keep the result of TryGetValue. int value; if (counts.TryGetValue("key", out value)) { counts["key"] = value + 1; Console.WriteLine("VALUE: " + counts["key"]); } } } Output VALUE: 1
ContainsKey. This example shows an increment on a Dictionary key. Two lookups are done when this occurs—one to get the value, and another to change it.ContainsKey

And: This is inefficient. Unlike the version with TryGetValue, we do an extra lookup to get "value" again.

C# program that uses ContainsKey using System; using System.Collections.Generic; class Program { static void Main() { var counts = new Dictionary<string, int>(); counts.Add("key", 0); // This causes an unneeded look up. if (counts.ContainsKey("key")) { counts["key"]++; Console.WriteLine("VALUE: " + counts["key"]); } } } Output VALUE: 1
ContainsKey, explanation. Here the increment does two lookups when we only need one. To increment the value in the Dictionary, we have to get it.
C# program that shows what ContainsKey does using System; using System.Collections.Generic; class Program { static void Main() { var counts = new Dictionary<string, int>(); counts.Add("key", 0); // This causes extra lookups. if (counts.ContainsKey("key")) { counts["key"] = counts["key"] + 1; Console.WriteLine("VALUE: " + counts["key"]); } } } Output VALUE: 1
Benchmark. We see how the TryGetValue method performs when used in the way in the above examples. We discover a performance benefit from eliding the last hash lookup.

Tip: Expert developers understand precisely what their code is doing. This is more important than making the code twice as fast.

Benchmark results for TryGetValue Use ContainsKey: 1700 ms Use TryGetValue: 1108 ms [faster]
Summary. You can rewrite Dictionary code to use TryGetValue instead of ContainsKey. It is good to avoid the increment or decrement operators here. Instead store the value with TryGetValue.Dictionary
© 2007-2019 Sam Allen. Every person is special and unique. Send bug reports to info@dotnetperls.com.
HomeSearch
Home
Dot Net Perls