namespace LibMatrix.Extensions; public static class DictionaryExtensions { public static bool ChangeKey(this IDictionary dict, TKey oldKey, TKey newKey) { if (!dict.Remove(oldKey, out var value)) return false; dict[newKey] = value; // or dict.Add(newKey, value) depending on ur comfort return true; } 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; } }