Reputation: 23
So I have created ICollection wrapper class over an array:
public class Product { public string name; }
Public class ProductCollection<T> : ICollection<T> where T : Product
{
T[] dataCollection;
public void Add(T item){...}
public void Clear(T item){...}
public void Containers(T item){...}
public void CopyTo(T item){...}
public IEnumerator<T> GetEnumerator() { return new Enumerator<T>(this); }
IEnumerator IEnumerable.GetEnumerator() { return new Enumerator<T>(this); }
}
class Enumerator<B> : IEnumerator<B> where B : Product
{
B[] collection;
public Enumerator(ProductCollection<B> collection)
{
this.collection = collection.dataCollection;
}
public B current...
public bool MoveNext()...
public void Reset()...
public void Dispose()
{
????
}
}
Is the Dispose method required here since the Product class does not contain any unmanaged resources only a string property?
Upvotes: 0
Views: 101
Reputation: 36541
Is the Dispose method required here since the Product class does not contain any unmanaged resources only a string property?
Yes, you need a dispose method since IEnumerator<T>
derives from IDisposable. But it is fine to leave the dispose method empty if there is no resources to dispose of.
But instead of writing your own Enumerator you should probably just use the one provided by the underlying array: ((IEnumerable<T>)datacollection).GetEnumerator();
. If you need to do something special you could use an iterator block. Even better would be to just use a List<Product>
instead of writing your own custom collection, there should rarely be any need to write a custom collection class.
Upvotes: 2