[{"data":1,"prerenderedAt":451},["ShallowReactive",2],{"blog:2009:dictionaryt-look-up-or-create-made-simpler":3,"blogMore-Development":437,"comments-dictionaryt-look-up-or-create-made-simpler":450},{"id":4,"title":5,"body":6,"category":420,"commentCount":152,"date":421,"description":422,"excerpt":423,"extension":424,"filenames":425,"hidden":426,"image":425,"meta":427,"minutes":82,"navigation":85,"path":428,"seo":429,"showCategory":425,"stem":430,"tags":431,"updated":425,"url":434,"wordCount":435,"__hash__":436},"content\u002Fblog\u002F2009\u002Fdictionaryt-look-up-or-create-made-simpler.md","Dictionary\u003CT> look-up or create made simpler",{"type":7,"value":8,"toc":418},"minimark",[9,18,197,200,250,253,408,414],[10,11,12,13,17],"p",{},"The design of a ",[14,15,16],"code",{},"Dictionary\u003CT>"," lends itself well to a caching or identification mechanism and as a result you often see code that looks like this:",[19,20,25],"pre",{"className":21,"code":22,"language":23,"meta":24,"style":24},"language-csharp shiki shiki-themes everforest-light dracula","private static Dictionary\u003Cstring, Employee> employees = new Dictionary\u003Cstring, Employee>();\n\npublic static Employee GetByName(string name) {\n  Employee employee;\n  if (!employees.TryGetValue(name, out employee)) {\n    employee = new Employee(whatever);\n    employees.Add(name, employee);\n  }\n  return employee;\n}\n","csharp","",[14,26,27,80,87,114,123,150,165,177,183,191],{"__ignoreMap":24},[28,29,32,36,39,43,47,51,54,57,60,63,67,69,71,73,75,77],"span",{"class":30,"line":31},"line",1,[28,33,35],{"class":34},"s9HRq","private",[28,37,38],{"class":34}," static",[28,40,42],{"class":41},"snuxY"," Dictionary",[28,44,46],{"class":45},"s6Vpi","\u003C",[28,48,50],{"class":49},"sXAHl","string",[28,52,53],{"class":45},", ",[28,55,56],{"class":41},"Employee",[28,58,59],{"class":45},"> employees ",[28,61,62],{"class":34},"=",[28,64,66],{"class":65},"smiwp"," new",[28,68,42],{"class":41},[28,70,46],{"class":45},[28,72,50],{"class":49},[28,74,53],{"class":45},[28,76,56],{"class":41},[28,78,79],{"class":45},">();\n",[28,81,83],{"class":30,"line":82},2,[28,84,86],{"emptyLinePlaceholder":85},true,"\n",[28,88,90,93,95,98,102,105,107,111],{"class":30,"line":89},3,[28,91,92],{"class":34},"public",[28,94,38],{"class":34},[28,96,97],{"class":41}," Employee",[28,99,101],{"class":100},"sS4Kt"," GetByName",[28,103,104],{"class":45},"(",[28,106,50],{"class":49},[28,108,110],{"class":109},"s7cAX"," name",[28,112,113],{"class":45},") {\n",[28,115,117,120],{"class":30,"line":116},4,[28,118,119],{"class":41},"  Employee",[28,121,122],{"class":45}," employee;\n",[28,124,126,129,132,135,138,141,144,147],{"class":30,"line":125},5,[28,127,128],{"class":65},"  if",[28,130,131],{"class":45}," (",[28,133,134],{"class":34},"!",[28,136,137],{"class":45},"employees.",[28,139,140],{"class":100},"TryGetValue",[28,142,143],{"class":45},"(name, ",[28,145,146],{"class":34},"out",[28,148,149],{"class":45}," employee)) {\n",[28,151,153,156,158,160,162],{"class":30,"line":152},6,[28,154,155],{"class":45},"    employee ",[28,157,62],{"class":34},[28,159,66],{"class":65},[28,161,97],{"class":41},[28,163,164],{"class":45},"(whatever);\n",[28,166,168,171,174],{"class":30,"line":167},7,[28,169,170],{"class":45},"    employees.",[28,172,173],{"class":100},"Add",[28,175,176],{"class":45},"(name, employee);\n",[28,178,180],{"class":30,"line":179},8,[28,181,182],{"class":45},"  }\n",[28,184,186,189],{"class":30,"line":185},9,[28,187,188],{"class":65},"  return",[28,190,122],{"class":45},[28,192,194],{"class":30,"line":193},10,[28,195,196],{"class":45},"}\n",[10,198,199],{},"It’s not that it is particularly difficult but it can be a bit error prone and when you’re doing it over and over. What would be nicer is something that let you do:",[19,201,203],{"className":21,"code":202,"language":23,"meta":24,"style":24},"public static Employee GetByName(string name) {\n  return employees.GetOrAdd(name, () => new Employee(whatever));\n}\n",[14,204,205,223,246],{"__ignoreMap":24},[28,206,207,209,211,213,215,217,219,221],{"class":30,"line":31},[28,208,92],{"class":34},[28,210,38],{"class":34},[28,212,97],{"class":41},[28,214,101],{"class":100},[28,216,104],{"class":45},[28,218,50],{"class":49},[28,220,110],{"class":109},[28,222,113],{"class":45},[28,224,225,227,230,233,236,239,241,243],{"class":30,"line":82},[28,226,188],{"class":65},[28,228,229],{"class":45}," employees.",[28,231,232],{"class":100},"GetOrAdd",[28,234,235],{"class":45},"(name, () ",[28,237,238],{"class":34},"=>",[28,240,66],{"class":65},[28,242,97],{"class":41},[28,244,245],{"class":45},"(whatever));\n",[28,247,248],{"class":30,"line":89},[28,249,196],{"class":45},[10,251,252],{},"Here’s an extension method to drop-in to a static class of your choosing that achieves just that.",[19,254,256],{"className":21,"code":255,"language":23,"meta":24,"style":24},"public static TDictionaryValue GetOrAdd\u003CTKey, TDictionaryValue>(\n  this IDictionary\u003CTKey, TDictionaryValue> dictionary,\n  TKey key,\n  Func\u003CTDictionaryValue> newValue\n) {\n  TDictionaryValue value;\n  if (!dictionary.TryGetValue(key, out value)) {\n    value = newValue.Invoke();\n    dictionary.Add(key, value);\n  }\n  return value;\n}\n",[14,257,258,284,309,319,333,337,345,366,382,392,396,403],{"__ignoreMap":24},[28,259,260,262,264,267,270,272,276,278,281],{"class":30,"line":31},[28,261,92],{"class":34},[28,263,38],{"class":34},[28,265,266],{"class":41}," TDictionaryValue",[28,268,269],{"class":100}," GetOrAdd",[28,271,46],{"class":45},[28,273,275],{"class":274},"sAO9U","TKey",[28,277,53],{"class":45},[28,279,280],{"class":274},"TDictionaryValue",[28,282,283],{"class":45},">(\n",[28,285,286,289,292,294,296,298,300,303,306],{"class":30,"line":82},[28,287,288],{"class":34},"  this",[28,290,291],{"class":41}," IDictionary",[28,293,46],{"class":45},[28,295,275],{"class":41},[28,297,53],{"class":45},[28,299,280],{"class":41},[28,301,302],{"class":45},"> ",[28,304,305],{"class":109},"dictionary",[28,307,308],{"class":45},",\n",[28,310,311,314,317],{"class":30,"line":89},[28,312,313],{"class":41},"  TKey",[28,315,316],{"class":109}," key",[28,318,308],{"class":45},[28,320,321,324,326,328,330],{"class":30,"line":116},[28,322,323],{"class":41},"  Func",[28,325,46],{"class":45},[28,327,280],{"class":41},[28,329,302],{"class":45},[28,331,332],{"class":109},"newValue\n",[28,334,335],{"class":30,"line":125},[28,336,113],{"class":45},[28,338,339,342],{"class":30,"line":152},[28,340,341],{"class":41},"  TDictionaryValue",[28,343,344],{"class":45}," value;\n",[28,346,347,349,351,353,356,358,361,363],{"class":30,"line":167},[28,348,128],{"class":65},[28,350,131],{"class":45},[28,352,134],{"class":34},[28,354,355],{"class":45},"dictionary.",[28,357,140],{"class":100},[28,359,360],{"class":45},"(key, ",[28,362,146],{"class":34},[28,364,365],{"class":45}," value)) {\n",[28,367,368,371,373,376,379],{"class":30,"line":179},[28,369,370],{"class":45},"    value ",[28,372,62],{"class":34},[28,374,375],{"class":45}," newValue.",[28,377,378],{"class":100},"Invoke",[28,380,381],{"class":45},"();\n",[28,383,384,387,389],{"class":30,"line":185},[28,385,386],{"class":45},"    dictionary.",[28,388,173],{"class":100},[28,390,391],{"class":45},"(key, value);\n",[28,393,394],{"class":30,"line":193},[28,395,182],{"class":45},[28,397,399,401],{"class":30,"line":398},11,[28,400,188],{"class":65},[28,402,344],{"class":45},[28,404,406],{"class":30,"line":405},12,[28,407,196],{"class":45},[10,409,410],{},[411,412,413],"em",{},"[)amien",[415,416,417],"style",{},"html pre.shiki code .s9HRq, html code.shiki .s9HRq{--shiki-default:#F57D26;--shiki-dark:#FF79C6}html pre.shiki code .snuxY, html code.shiki .snuxY{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#8BE9FD;--shiki-dark-font-style:italic}html pre.shiki code .s6Vpi, html code.shiki .s6Vpi{--shiki-default:#5C6A72;--shiki-dark:#F8F8F2}html pre.shiki code .sXAHl, html code.shiki .sXAHl{--shiki-default:#3A94C5;--shiki-dark:#FF79C6}html pre.shiki code .smiwp, html code.shiki .smiwp{--shiki-default:#F85552;--shiki-dark:#FF79C6}html pre.shiki code .sS4Kt, html code.shiki .sS4Kt{--shiki-default:#8DA101;--shiki-dark:#50FA7B}html pre.shiki code .s7cAX, html code.shiki .s7cAX{--shiki-default:#5C6A72;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html pre.shiki code .sAO9U, html code.shiki .sAO9U{--shiki-default:#3A94C5;--shiki-default-font-style:inherit;--shiki-dark:#FFB86C;--shiki-dark-font-style:italic}",{"title":24,"searchDepth":82,"depth":82,"links":419},[],"Development","2009-08-04T16:24:04+00:00","The design of a Dictionary\u003CT> lends itself well to a caching or identification mechanism and as a result you often see code that looks like this:","[object Object]","md",null,false,{},"\u002Fblog\u002F2009\u002Fdictionaryt-look-up-or-create-made-simpler",{"title":5,"description":422},"blog\u002F2009\u002Fdictionaryt-look-up-or-create-made-simpler",[432,433],".NET","C#","\u002Fblog\u002F2009\u002Fdictionaryt-look-up-or-create-made-simpler\u002F",281,"vjQK0-mKKXeGoHd-J17pCfUW4xQs1dlxZo_nJi9y9P4",[438,442,446],{"title":439,"date":440,"url":441},"Transactions in the MongoDB EF Core Provider","2025-10-25","\u002Fblog\u002F2025\u002Fmongodb-explicit-transactions\u002F",{"title":443,"date":444,"url":445},"Queryable Encryption with the MongoDB EF Core Provider","2025-09-22","\u002Fblog\u002F2025\u002Fmongodb-queryable-encryption\u002F",{"title":447,"date":448,"url":449},"Lazy Loading with EF Core Proxies","2025-04-02","\u002Fblog\u002F2025\u002Fef-proxies\u002F",[],1780900527411]