Pretty simple class but just want to make sure I've approached it correctly.
public class DynamicArray<T> : IEnumerable<T>, ICollection<T>
{
private T[] _items;
private int _size;
private const int _growFactor = 2;
public DynamicArray()
{
_items = new T[4];
}
public int Count
{
get
{
return _size;
}
}
public bool IsReadOnly
{
get
{
return false;
}
}
public void Add(T item)
{
if (_size + 1 > _items.Length)
{
var newArray = new T[_items.Length * _growFactor];
_items.CopyTo(newArray, 0);
_items = newArray;
}
_items[_size] = item;
_size++;
}
public void Clear()
{
_items = new T[2];
_size = 0;
}
public bool Contains(T item)
{
foreach(var value in _items)
{
if (value.Equals(item))
{
return true;
}
}
return false;
}
public void CopyTo(T[] array, int arrayIndex)
{
_items.CopyTo(array, arrayIndex);
}
public IEnumerator<T> GetEnumerator()
{
for(var i = 0; i < _size; i++)
{
yield return _items[i];
}
}
public bool Remove(T item)
{
for(var i = 0; i < _items.Length; i++)
{
if (_items[i].Equals(item))
{
_items[i] = default(T);
_size--;
return true;
}
}
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
}