using System; using System.Collections; using System.Linq; namespace Common { public class EnumIndexedArray : IEnumerable where TIndex : Enum // requires C# 7.3 or later { private readonly T[] array; private readonly int lower; public EnumIndexedArray() { lower = Convert.ToInt32(Enum.GetValues(typeof(TIndex)).Cast().Min()); int upper = Convert.ToInt32(Enum.GetValues(typeof(TIndex)).Cast().Max()); array = new T[1 + upper - lower]; } public T this[TIndex key] { get => array[Convert.ToInt32(key) - lower]; set => array[Convert.ToInt32(key) - lower] = value; } public IEnumerator GetEnumerator() { return Enum.GetValues(typeof(TIndex)).Cast().Select(i => this[i]).GetEnumerator(); } public void Assign(EnumIndexedArray source) { for (int i = 0; i < source.array.Length; i++) { array[i] = source.array[i]; } } } }