Home
C#
Dictionary TryGetValue (Get Value)
This page was last reviewed on Oct 25, 2023.
Dot Net Perls
TryGetValue. This method optimizes Dictionary usage. It gets a value (at a key) from a Dictionary. And it eliminates unneeded lookups, making programs better.
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.
Dictionary
Dictionary GetValueOrDefault
TryGetValue, example. We can rewrite programs that use ContainsKey with TryGetValue. By combining 2 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.
Detail 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.
using System; using System.Collections.Generic; 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"]); }
VALUE: 1
Out type. The second argument to TryGetValue is an out parameter. We can specify the type (here it is bool) directly in the call to the TryGetValue method.
out
using System; using System.Collections.Generic; var ids = new Dictionary<string, bool>() { { "X1", true } }; // We can specify the "out" type by argument. if (ids.TryGetValue("X1", out bool result)) { Console.WriteLine($"VALUE: {result}"); }
VALUE: True
Benchmark, TryGetValue. This benchmark compares the TryGetValue method against ContainsKey and indexer. It compares 1 lookup against 2 lookups.
Benchmark
Version 1 Use ContainsKey. If the key exists, access the value again in a separate lookup statement.
Dictionary ContainsKey
Version 2 This version of the code uses TryGetValue, and stores the result value which is then used for the sum.
Result It is faster to just use the TryGetValue method and access the already-known value from a local variable.
using System; using System.Collections.Generic; using System.Diagnostics; const int _max = 10000000; var test = new Dictionary<string, int>(); test["key"] = 1; int sum = 0; // Version 1: use ContainsKey and access the key again for its value. var s1 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (test.ContainsKey("key")) { sum += test["key"]; } } s1.Stop(); // Version 2: use TryGetValue and use the key already accessed. var s2 = Stopwatch.StartNew(); for (int i = 0; i < _max; i++) { if (test.TryGetValue("key", out int result)) { sum += result; } } s2.Stop(); Console.WriteLine(((double)(s1.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns")); Console.WriteLine(((double)(s2.Elapsed.TotalMilliseconds * 1000000) / _max).ToString("0.00 ns"));
38.11 ns ContainsKey, indexer 21.16 ns TryGetValue
Summary. We 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.
Dot Net Perls is a collection of tested code examples. Pages are continually updated to stay current, with code correctness a top priority.
Sam Allen is passionate about computer languages. In the past, his work has been recommended by Apple and Microsoft and he has studied computers at a selective university in the United States.
This page was last updated on Oct 25, 2023 (edit).
Home
Changes
© 2007-2024 Sam Allen.