阵列上的偶像中处置方法
因此,我已经通过数组创建了Icollection包装器类:
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()
{
????
}
}
此处是否需要使用Dispose方法,因为产品类不包含任何不受管理的资源?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,您需要一种处置方法,因为
ienumerator&lt; t&gt;
源自iDisposable。但是,如果没有资源可处置,则可以将处置方法留空是可以的。但是,而不是编写自己的枚举器,而是应该只使用基础数组提供的一个:
(((iEnumerable&lt; t&gt;)dataCollection).getEnumerator();
。如果您需要做一些特别的事情,则可以使用。甚至最好是使用list&lt; product&gt;
而不是编写自己的自定义集合,因此很少需要编写自定义集合类。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 aList<Product>
instead of writing your own custom collection, there should rarely be any need to write a custom collection class.