阵列上的偶像中处置方法

发布于 2025-02-09 04:20:56 字数 938 浏览 0 评论 0 原文

因此,我已经通过数组创建了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方法,因为产品类不包含任何不受管理的资源?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

撧情箌佬 2025-02-16 04:20:56

由于产品类不包含任何不管理资源的字符串属性?

,此处是否需要处置方法?

是的,您需要一种处置方法,因为 ienumerator&lt; t&gt; 源自iDisposable。但是,如果没有资源可处置,则可以将处置方法留空是可以的。

但是,而不是编写自己的枚举器,而是应该只使用基础数组提供的一个:(((iEnumerable&lt; t&gt;)dataCollection).getEnumerator(); 。如果您需要做一些特别的事情,则可以使用。甚至最好是使用 list&lt; product&gt; 而不是编写自己的自定义集合,因此很少需要编写自定义集合类。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文