diff --git a/OmegaLeo.HelperLib/Models/NeoDictionary.cs b/OmegaLeo.HelperLib/Models/NeoDictionary.cs index 12861fb..fbcbec9 100644 --- a/OmegaLeo.HelperLib/Models/NeoDictionary.cs +++ b/OmegaLeo.HelperLib/Models/NeoDictionary.cs @@ -90,6 +90,54 @@ namespace GameDevLibrary.Models } public void RemoveAt(int index) => Items.RemoveAt(index); + + public void Replace(TKey key, TValue value) + { + if (HasKey(key)) + { + this[key] = value; + } + } + + public bool HasKey(TKey key) => Items.Any(x => x.Key.Equals(key)); + + public TValue this[TKey key] + { + get + { + if (Items == null) + { + Items = new List>(); + Items.Add(new NeoDictionaryItem(key, (TValue)Activator.CreateInstance(typeof(TValue)))); + } + + try + { + var value = Items.FirstOrDefault(x => EqualityComparer.Default.Equals(x.Key, key))!.Value; + + return value ?? (TValue)Activator.CreateInstance(typeof(TValue)); + } + catch (Exception e) + { + return (TValue)Activator.CreateInstance(typeof(TValue)); + } + } + set + { + if (HasKey(key)) + { + var item = Items.FirstOrDefault(x => EqualityComparer.Default.Equals(x.Key, key)); + var index = Items.IndexOf(item); + Items[index] = new NeoDictionaryItem(key, value); + } + else + { + Items.Add(new NeoDictionaryItem(key, value)); + } + } + } + + public void Clear() => Items.Clear(); } [Serializable]