using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace MyGenericList { public class MyGenericList where T : IComparable { const int defaultCapacity = 6; private T[] list = null; public MyGenericList(int capacity = defaultCapacity) { this.Count = 0; this.Capacity = capacity; list = new T[capacity]; } private int capacity; public int Capacity { get { return capacity; } private set { capacity = value; } } private int count; public int Count { get { return count; } set { count = value; } } public void Add(T item) { this.Count++; this.Resize(this.Count); list[count - 1] = item; } public void RemoveAt(int index) { if (index > Count || index < 0) { throw new IndexOutOfRangeException("Index is out of range."); } this.Count--; this.Resize(this.Count); Array.Copy(this.list, index, this.list, index--, this.Count - index); this.list[count] = default(T); } public void Insert(int index, T element) { if (index > Count || index < 0) { throw new IndexOutOfRangeException("Index is out of range."); } this.Count++; this.Resize(this.Count); Array.Copy(this.list, index, this.list, index++, this.Count - index -1); this.list[index - 1] = element; } public int FindIndex(T item) { return Array.IndexOf(this.list, item); } public bool Contains(T item) { return this.list.Contains(item); } public void Clear() { this.Count = 0; this.Capacity = defaultCapacity; this.list = new T[Capacity]; } private void Resize(int currentCounter) { if (currentCounter > this.Capacity) { this.Capacity = this.Capacity * 2; Array.Resize(ref this.list, this.Capacity); } } } }