I post the below code as review/answer to a question in codereview. (Custom Dynamic Array) however, seems the answer itself having issues looking at the comments that I received. I would appreciate your valuable review comments:
public class DynamicArray<T> : IEnumerable<T>, ICollection<T>
{
private T[] _items;
public DynamicArray()
{
}
public int Count
{
get
{
if (_items == null)
return 0;
return _items.Length;
}
}
public bool IsReadOnly => false;
public void Add(T item)
{
if(item==null)
throw new ArgumentException("item");
if (_items == null)
{
_items = new T[1];
}
else
{
Array.Resize(ref _items,_items.Length+1);
}
_items[_items.Length - 1] = item;
}
public void Clear()
{
_items = null;
}
public bool Contains(T item)
{
if (_items == null)
return false;
return _items.Contains(item);
}
public void CopyTo(T[] array, int arrayIndex)
{
_items.CopyTo(array, arrayIndex);
}
public IEnumerator<T> GetEnumerator()
{
if (_items == null)
return Enumerable.Empty<T>().GetEnumerator();
return _items.AsEnumerable().GetEnumerator();
}
public bool Remove(T item)
{
if (item == null)
return false;
if(_items==null)
return false;
var temp = _items.Where(i => !i.Equals(item));
_items = temp.ToArray();
return true;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}