A Small Generic Array Class
using System; using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; [Serializable] public class MiniArray<T> : IEnumerable<T> { public T[] Array; public MiniArray() : this(101) { } public MiniArray(int cap) { Count = 0; Array = new T[cap]; } public int Count { get; private set; } public T this[int index] { get { if (index > Array.Length) throw new Exception("Error: Index out of range."); return Array[index]; } set { EnsureSize(); Array[index] = value; Count++; } } IEnumerator<T> IEnumerable<T>.GetEnumerator() { return new Enumerator<T>(this); } IEnumerator IEnumerable.GetEnumerator() { return new Enumerator<T>(this); } private void EnsureSize() { if (Count >= Array.Length) { var NewLength = Array.Length == 0 ? 1 : Array.Length * 2; var newtArray = new T[NewLength]; System.Array.Copy(Array, 0, newtArray, 0, Array.Length); Array = newtArray; } } public void Add(T item) { EnsureSize(); Array[Count] = item; Count++; } public T[] ToArray() { var newtArray = new T[Count]; System.Array.Copy(Array, 0, newtArray, 0, Count); return newtArray; } public IEnumerable<T> All() { for (var i = 0; i < Count; ++i) yield return Array[i]; } public void Clean() { var newtArray = new T[Count]; System.Array.Copy(Array, 0, newtArray, 0, Count); Array = newtArray; } public void Clear() { System.Array.Clear(Array, 0, Count); Count = 0; } } [Serializable] [StructLayout(LayoutKind.Sequential)] public struct Enumerator<T> : IEnumerator<T> { private readonly MiniArray<T> thing; private int index; internal Enumerator(MiniArray<T> thing) { this.thing = thing; index = 0; Current = default; } public void Dispose() { } public bool MoveNext() { var tthing = thing; if (index < tthing.Count) { Current = tthing[index]; index++; return true; } index = thing.Count + 1; Current = default; return false; } public T Current { get; private set; } object IEnumerator.Current => Current; void IEnumerator.Reset() { index = 0; Current = default; } }