{"id":3746,"date":"2020-05-14T08:30:00","date_gmt":"2020-05-14T07:30:00","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3746"},"modified":"2026-10-06T14:24:39","modified_gmt":"2026-10-06T13:24:39","slug":"c-dictionary-getkey","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-dictionary-getkey\/","title":{"rendered":"C# Dictionary GetKey"},"content":{"rendered":"\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"csharp\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">public static class DictionaryExtensions\n{\n    public static T GetKey&lt;T, K>(this IDictionary&lt;K, T> dict, K? key, T defValue = default) where K : struct\n    {\n        return key.HasValue &amp;&amp; dict.TryGetValue(key.Value, out T value) ? value : defValue;\n    }\n\n    public static T GetKey&lt;T, K>(this IDictionary&lt;K, T> dict, K key, T defValue = default)\n    {\n        return key != null &amp;&amp; dict.TryGetValue(key, out T value) ? value : defValue;\n    }\n}\n\n\/\/ for nullable-guid keys in dotnet 8:\npublic static class DictionaryExtensions\n{\n\tpublic static T GetKey&lt;T, K>(this IDictionary&lt;K, T> dict, K? key, T defValue = default)\n\t\twhere K : struct\n\t{\n\t\treturn key.HasValue &amp;&amp; dict.TryGetValue(key.Value, out var value) ? value : defValue;\n\t}\n\n\tpublic static T GetKey&lt;T, K>(this IDictionary&lt;K?, T> dict, K? key, T defValue = default)\n\t\twhere K : struct\n\t{\n\t\treturn key.HasValue &amp;&amp; dict.TryGetValue(key, out var value) ? value : defValue;\n\t}\n\n\tpublic static T GetKey&lt;T, K>(this IDictionary&lt;K, T> dict, K key, T defValue = default)\n\t\twhere K : class\n\t{\n\t\treturn key != null &amp;&amp; dict.TryGetValue(key, out var value) ? value : defValue;\n\t}\n}<\/pre>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[6,4,1],"tags":[],"class_list":["post-3746","post","type-post","status-publish","format-standard","hentry","category-dotnet","category-programming","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3746","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/comments?post=3746"}],"version-history":[{"count":7,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3746\/revisions"}],"predecessor-version":[{"id":10345,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3746\/revisions\/10345"}],"wp:attachment":[{"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/media?parent=3746"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/categories?post=3746"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/tags?post=3746"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}