diff --git a/TechbloxModdingAPI/Utility/WeakDictionary.cs b/TechbloxModdingAPI/Utility/WeakDictionary.cs index 35e55a5..6c4cf57 100644 --- a/TechbloxModdingAPI/Utility/WeakDictionary.cs +++ b/TechbloxModdingAPI/Utility/WeakDictionary.cs @@ -7,7 +7,7 @@ namespace TechbloxModdingAPI.Utility { public class WeakDictionary : IDictionary where TValue : class { - private Dictionary> _dictionary = new Dictionary>(); + private readonly Dictionary> _dictionary = new(); public IEnumerator> GetEnumerator() { @@ -19,53 +19,19 @@ namespace TechbloxModdingAPI.Utility } } - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public void Add(KeyValuePair item) - { - Add(item.Key, item.Value); - } - - public void Clear() - { - _dictionary.Clear(); - } - - public bool Contains(KeyValuePair item) - { - return TryGetValue(item.Key, out var value) && item.Value == value; - } - - public void CopyTo(KeyValuePair[] array, int arrayIndex) - { + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public void Add(KeyValuePair item) => Add(item.Key, item.Value); + public void Clear() => _dictionary.Clear(); + public bool Contains(KeyValuePair item) => + TryGetValue(item.Key, out var value) && item.Value == value; + public void CopyTo(KeyValuePair[] array, int arrayIndex) => throw new System.NotImplementedException(); - } - - public bool Remove(KeyValuePair item) - { - return Contains(item) && Remove(item.Key); - } - + public bool Remove(KeyValuePair item) => Contains(item) && Remove(item.Key); public int Count => _dictionary.Count; public bool IsReadOnly => false; - - public bool ContainsKey(TKey key) - { - return TryGetValue(key, out _); - } - - public void Add(TKey key, TValue value) - { - _dictionary.Add(key, new WeakReference(value)); - } - - public bool Remove(TKey key) - { - return _dictionary.Remove(key); - } + public bool ContainsKey(TKey key) => TryGetValue(key, out _); + public void Add(TKey key, TValue value) => _dictionary.Add(key, new WeakReference(value)); + public bool Remove(TKey key) => _dictionary.Remove(key); public bool TryGetValue(TKey key, out TValue value) { @@ -83,56 +49,55 @@ namespace TechbloxModdingAPI.Utility set => _dictionary[key] = new WeakReference(value); } - public ICollection Keys => _dictionary.Keys; + public ICollection Keys => new KeyCollection(this); public ICollection Values => new ValueCollection(this); - public class ValueCollection : ICollection, IReadOnlyCollection + public class KeyCollection : ICollection, IReadOnlyCollection { - private WeakDictionary _dictionary; - internal ValueCollection(WeakDictionary dictionary) + private readonly WeakDictionary _dictionary; + internal KeyCollection(WeakDictionary dictionary) { _dictionary = dictionary; } - public IEnumerator GetEnumerator() + public IEnumerator GetEnumerator() { using var enumerator = _dictionary.GetEnumerator(); while (enumerator.MoveNext()) - { - yield return enumerator.Current.Value; - } - } - - IEnumerator IEnumerable.GetEnumerator() - { - return GetEnumerator(); - } - - public void Add(TValue item) - { - throw new NotSupportedException("The value collection is read only."); - } - - public void Clear() - { - throw new NotSupportedException("The value collection is read only."); - } - - public bool Contains(TValue item) - { - return _dictionary.Any(kv => kv.Value == item); + yield return enumerator.Current.Key; } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public void Add(TKey item) => throw new NotSupportedException("The key collection is read only."); + public void Clear() => throw new NotSupportedException("The key collection is read only."); + public bool Contains(TKey item) => _dictionary.ContainsKey(item); + public void CopyTo(TKey[] array, int arrayIndex) => throw new NotImplementedException(); + public bool Remove(TKey item) => throw new NotSupportedException("The key collection is read only."); + public int Count => _dictionary.Count; + public bool IsReadOnly => true; + } - public void CopyTo(TValue[] array, int arrayIndex) + public class ValueCollection : ICollection, IReadOnlyCollection + { + private readonly WeakDictionary _dictionary; + internal ValueCollection(WeakDictionary dictionary) { - throw new NotImplementedException(); + _dictionary = dictionary; } - public bool Remove(TValue item) + public IEnumerator GetEnumerator() { - throw new NotSupportedException("The value collection is read only."); + using var enumerator = _dictionary.GetEnumerator(); + while (enumerator.MoveNext()) + yield return enumerator.Current.Value; } + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); + public void Add(TValue item) => throw new NotSupportedException("The value collection is read only."); + public void Clear() => throw new NotSupportedException("The value collection is read only."); + public bool Contains(TValue item) => _dictionary.Any(kv => kv.Value == item); + public void CopyTo(TValue[] array, int arrayIndex) => throw new NotImplementedException(); + public bool Remove(TValue item) => throw new NotSupportedException("The value collection is read only."); public int Count => _dictionary.Count; public bool IsReadOnly => true; }