From 2e89a0717a60598904c92499adb7e01b2075fbb9 Mon Sep 17 00:00:00 2001 From: "Emma@Rory&" Date: Wed, 26 Jul 2023 21:02:50 +0200 Subject: MRU desktop start --- .../Extensions/DictionaryExtensions.cs | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) (limited to 'MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs') diff --git a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs index c51baec..78bbdfa 100644 --- a/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs +++ b/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs @@ -10,4 +10,24 @@ public static class DictionaryExtensions { dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort return true; } -} \ No newline at end of file + + public static Y GetOrCreate(this IDictionary dict, X key) where Y : new() { + if (dict.TryGetValue(key, out var value)) { + return value; + } + + value = new Y(); + dict.Add(key, value); + return value; + } + + public static Y GetOrCreate(this IDictionary dict, X key, Func valueFactory) { + if (dict.TryGetValue(key, out var value)) { + return value; + } + + value = valueFactory(key); + dict.Add(key, value); + return value; + } +} -- cgit 1.5.1