{"id":3746,"date":"2020-05-14T08:30:00","date_gmt":"2020-05-14T07:30:00","guid":{"rendered":"https:\/\/solidt.eu\/site\/?p=3746"},"modified":"2025-12-24T11:50:11","modified_gmt":"2025-12-24T10:50:11","slug":"c-dictionary-getkey","status":"publish","type":"post","link":"https:\/\/solidt.eu\/site\/c-dictionary-getkey\/","title":{"rendered":"C# Dictionary GetKey"},"content":{"rendered":"\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">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}<\/pre><\/div>\n\n\n\n<p>Old or in some cases needed?<\/p>\n\n\n\n<div style=\"height: 250px; position:relative; margin-bottom: 50px;\" class=\"wp-block-simple-code-block-ace\"><pre class=\"wp-block-simple-code-block-ace\" style=\"position:absolute;top:0;right:0;bottom:0;left:0\" data-mode=\"csharp\" data-theme=\"monokai\" data-fontsize=\"14\" data-lines=\"Infinity\" data-showlines=\"true\" data-copy=\"false\">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 dict.TryGetValue(key, 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) where K : struct\n    {\n        return key.HasValue &amp;&amp; dict.TryGetValue(key, 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) 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 GetKeyC&lt;T, K>(this IDictionary&lt;K, T> dict, K key, T defValue = default) where K : class\n    {\n        return key != null &amp;&amp; dict.TryGetValue(key, out T value) ? value : defValue;\n    }\n}<\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Old or in some cases needed?<\/p>\n","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":6,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3746\/revisions"}],"predecessor-version":[{"id":10002,"href":"https:\/\/solidt.eu\/site\/wp-json\/wp\/v2\/posts\/3746\/revisions\/10002"}],"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}]}}