about summary refs log tree commit diff
path: root/MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs
diff options
context:
space:
mode:
authorEmma@Rory& <root@rory.gay>2023-07-26 21:02:50 +0200
committerEmma@Rory& <root@rory.gay>2023-07-26 21:02:50 +0200
commit2e89a0717a60598904c92499adb7e01b2075fbb9 (patch)
tree56b9b7f07180154d9d98dafa91d7c16a0a872100 /MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs
parentStart of nix flake (diff)
downloadMatrixUtils-2e89a0717a60598904c92499adb7e01b2075fbb9.tar.xz
MRU desktop start
Diffstat (limited to '')
-rw-r--r--MatrixRoomUtils.Core/Extensions/DictionaryExtensions.cs22
1 files changed, 21 insertions, 1 deletions
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<X, Y>(this IDictionary<X, Y> 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<X, Y>(this IDictionary<X, Y> dict, X key, Func<X, Y> valueFactory) { + if (dict.TryGetValue(key, out var value)) { + return value; + } + + value = valueFactory(key); + dict.Add(key, value); + return value; + } +}