为什么IEnumerator的< T>自IDisposable继承,而非通用IEnumerator的不? [英] Why does IEnumerator<T> inherit from IDisposable while the non-generic IEnumerator does not?

查看:193
本文介绍了为什么IEnumerator的< T>自IDisposable继承,而非通用IEnumerator的不?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我注意到,普通的的IEnumerator< T> 继承自IDisposable,但非通用接口的IEnumerator没有。为什么这样设计?

I noticed that the generic IEnumerator<T> inherits from IDisposable, but the non-generic interface IEnumerator does not. Why is it designed in this way?

一般情况下,我们使用foreach语句要经过一个的IEnumerator&LT; T&GT; 实例。的foreach的产生code实际上有尝试-finally块,在最后调用Dispose()方法。

Usually, we use foreach statement to go through a IEnumerator<T> instance. The generated code of foreach actually has try-finally block that invokes Dispose() in finally.

推荐答案

基本上,这是一个疏忽。在C#1.0,​​的foreach 从不的名为的Dispose 1 。用C#1.2(在VS2003推出 - 有没有1.1,奇怪)的foreach 开始在最后块是否检查他们不得不这样做的,因为回顾性使得的IEnumerator 延长的IDisposable 的迭代器> IDisposable的就已经打破了大家的实施的IEnumerator 。如果他们会制定出它的有用的foreach 处置摆在首位的迭代器,我敢肯定,的IEnumerator 的IDisposable >会延长。

Basically it was an oversight. In C# 1.0, foreach never called Dispose 1. With C# 1.2 (introduced in VS2003 - there's no 1.1, bizarrely) foreach began to check in the finally block whether or not the iterator implemented IDisposable - they had to do it that way, because retrospectively making IEnumerator extend IDisposable would have broken everyone's implementation of IEnumerator. If they'd worked out that it's useful for foreach to dispose of iterators in the first place, I'm sure IEnumerator would have extended IDisposable.

在C#2.0和.NET 2.0出来了,但是,他们有一个新的机会 - 新界面,新的继承。这使得很多更有意义的接口扩展的IDisposable ,这样你就不需要在finally块的执行时间检查,而现在的编译器知道如果迭代器是的IEnumerator&LT; T&GT; 它可以发射到的处置无条件呼叫

When C# 2.0 and .NET 2.0 came out, however, they had a fresh opportunity - new interface, new inheritance. It makes much more sense to have the interface extend IDisposable so that you don't need an execution-time check in the finally block, and now the compiler knows that if the iterator is an IEnumerator<T> it can emit an unconditional call to Dispose.

编辑:这对的Dispose 在迭代的最后(但它结束)被称为非常有用的。这意味着迭代器可以抓住资源 - 这使得它可行的它,说,读一行文件一行。迭代器块生成的Dispose 实现,这些都是确保任何最后有关的迭代器的执行当前点块当它处理被执行 - 所以你可以写迭代器和清理在正常code应适当发生

It's incredibly useful for Dispose to be called at the end of iteration (however it ends). It means the iterator can hold on to resources - which makes it feasible for it to, say, read a file line by line. Iterator blocks generator Dispose implementations which make sure that any finally blocks relevant to the "current point of execution" of the iterator are executed when it's disposed - so you can write normal code within the iterator and clean-up should happen appropriately.

1 回首1.0规范,它已经被指定。我还没有能够验证这一早些时候的发言了1.0实现没叫的Dispose

1 Looking back at the 1.0 spec, it was already specified. I haven't yet been able to verify this earlier statement that the 1.0 implementation didn't call Dispose.

这篇关于为什么IEnumerator的&LT; T&GT;自IDisposable继承,而非通用IEnumerator的不?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆