问题描述
我正在使用.NET 2.0并查看IEnumerator< T>的文档.文档的一部分说Dispose()方法不依赖于T,仅出现在非通用接口中.虽然我同意它不依赖于T,但仅依赖于通用接口. IEnumerator并非仅从IDisposable派生.做.由于实现IEnumerator< T>的对象实现IDisposable不应是IEnumerator< T>的某些方法.和它的IEnumerator方法抛出ObjectDisposedException吗?看文档,他们都不做.
,谢谢,
尼克
I'm using .NET 2.0 and looking at the docs for IEnumerator<T>. One part of the docs say that Dispose() method does not depend on T and only appears in non-generic interface. While I'll agree it's not dependent on T, it is only on the generic interface. IEnumerator does not derive from IDisposable only IEnumerator<T> does. Since an object implementing IEnumerator<T> implements IDisposable shouldn't some of the methods of IEnumerator<T> and its IEnumerator methods throw an ObjectDisposedException? Looking at the docs none of them do.
Thanks,
Nick
推荐答案
嗨尼克,
我们在谈论接口,对吧?就从接口派生的对象实现此接口而言,该对象在形式上与接口协定一致. IEnumerable T源自IDisposable而非假设IDisposableT (outch!),因此IDisposable接口仅要求派生自IEnumerable< T>的对象.实现具有签名 public void Dispose()的方法.这与T实现或不实现IDisposable无关.
当你看例如进入List< T>的实现您会看到从IEnumerator< T> IDisposable,IEnumerator派生的List< T.Enumerator"具有* empty *主体的Dispose方法.没关系.无论如何应该处置什么?枚举器以透明方式浏览对象序列.当我们调用MoveNext()时,它只是将索引更新为T [idx ++],并将当前的T项设置为T [idx ++]返回的值.如果退回的物品是同时处置的,则不是调查员的业务(请参阅关注点分离).恕我直言.
你让我好奇了.您在说什么文档?
Marcel
Hi Nick,
We're talking about interfaces, right? As far as an object derived from an interface implements this interface, the object is formally consistent with the interface contract. IEnumerable<T> derives from IDisposable not from a hypothetic IDisposable<T> (outch!), so the IDisposable interface only requires that the object derived from IEnumerable<T> implements a method with the signature public void Dispose(). This has nothing to do with T implementing or not implementing IDisposable.
When you look e.g. into the implementation of List<T> you can see that the List<T>.Enumerator that derives from IEnumerator<T>, IDisposable, IEnumerator has a Dispose method with an *empty* body. It's OK. What should one dispose anyway? The enumerator is navigating a sequence of objects in a transparent manner. When we call MoveNext(), it simply updates an index into an T[idx++] and sets the current T item to whatever T[idx++] returns. If the item returned was meantime disposed, that's not the enumerator's business (see separation of concerns). IMHO.
You made me curios. What docs are you talking about?
Marcel
这篇关于好像是IEnumerator< T>的某些方法.应该抛出ObjectDisposedException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!