Skip to content

Commit 4774298

Browse files
committed
Introduced extension method for dictionaries for getting the desired value or a default value if the key does not exist
1 parent 973029d commit 4774298

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
namespace System.Collections.Generic;
2+
3+
internal static class DictionaryExtensions
4+
{
5+
public static TValue? GetOrDefault<TKey, TValue>(this IDictionary<TKey, TValue> dictionary, TKey key, TValue? defaultValue = default) =>
6+
dictionary.TryGetValue(key, out var value) ? value : defaultValue;
7+
}

0 commit comments

Comments
 (0)