为什么在枚举类中的reset()方法必须引发NotSupportedException()? [英] Why the Reset() method on Enumerator class must throw a NotSupportedException()?

查看:289
本文介绍了为什么在枚举类中的reset()方法必须引发NotSupportedException()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从我所看到的<一个href="http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx">http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx,和文章乔恩飞碟双向,在C#规范本身说。会是什么原因呢?

From what I saw on http://csharpindepth.com/Articles/Chapter6/IteratorBlockImplementation.aspx, and article by Jon Skeet, the c# specification itself says that. What would be the reason?

推荐答案

这不是我该怎么读<一href="http://download.microsoft.com/download/3/8/8/388e7205-bc10-4226-b2a8-75351c669b09/CSharp%20Language%20Specification.doc">C# SPEC [Word文档。第10.14.4枚举器对象,规定:

That's not how I read the C# spec [Word doc]. Section 10.14.4 "Enumerator objects", states:

... [E]分子对象不支持IEnumerator.Reset方法。调用此方法将导致System.NotSupportedException抛出。

...[E]numerator objects do not support the IEnumerator.Reset method. Invoking this method causes a System.NotSupportedException to be thrown.

然而,此单元(和语句)是特定于枚举物,其被定义为:

However, this section (and statement) is specific to "enumerator objects", which is defined as:

当一个成员函数返回一个枚举器接口类型使用迭代器块,调用函数成员不会立即执行code在迭代器块实现。相反,枚举器对象创建并返回。

When a function member returning an enumerator interface type is implemented using an iterator block, invoking the function member does not immediately execute the code in the iterator block. Instead, an enumerator object is created and returned.

在换句话说,枚举器对象是的编译器的生成的IEnumerator 1 。有对每一个没有任何限制的IEnumerator ,刚刚从迭代器块所产生的那些(又名收益率)。

In other words, an "enumerator object" is a compiler generated IEnumerator1. There's no restrictions on every IEnumerator, just the ones generated from iterator blocks (aka yield).

至于为什么?我怀疑,因为它有点不可能做到在一般情况下 - 不保存每一个值,那随之而来的内存限制。再加上一个事实,即 IEnumerator.Reset()很少使用,而且的MSDN特别呼吁指出,它不需要实现

As for why? I'd suspect because it's somewhat impossible to do in the general case - without saving every value and the consequent memory limitations of that. Combine that with the fact that IEnumerator.Reset() is rarely used (when's the last time that you Reset an enumerator?) and that MSDN specifically calls out that it need not be implemented:

Reset方法提供了COM互操作性。它不一定需要被执行;相反,实施者可以简单地抛出一个NotSupportedException异常。

The Reset method is provided for COM interoperability. It does not necessarily need to be implemented; instead, the implementer can simply throw a NotSupportedException.

,你会得到削减了很多的复杂性,没有任何人真正注意到。

and you get to cut out a lot of complexity without anyone really noticing.

至于的需要的,它抛出 2 ,我想这只是不是让实现者决定简单。国际海事组织,这是一个有点多,要求扔 - 有可能是合理的情况下,一个编译器(或其他实施 1 )可能会产生复位方法,但我不把它看作是一个真正的问题无论是。

As for requiring that it throw2, I suppose it's just simpler than letting the implementor decide. IMO, it's a bit much to require the throw - there may be reasonable cases that a compiler (or other implementation1) could generate a Reset method for, but I don't see it as being a real problem either.

1 从技术上讲,该规范也开启其他实现的可能性:

1 Technically, the spec leaves open the possibility of other implementations:

这是枚举器对象通常是封装了code迭代器块,并实现了枚举器接口,编译器生成的枚举器类的实例,但实施的其他方法是可行的。

An enumerator object is typically an instance of a compiler-generated enumerator class that encapsulates the code in the iterator block and implements the enumerator interfaces, but other methods of implementation are possible.

但我不知道有任何其他的具体实现。无论如何,都符合,一个枚举器对象的其他实现将不得不放弃 NotSupportedException异常

but I'm not aware of any other concrete implementations. Regardless, to be compliant, other implementations of an "enumerator object" would have to throw NotSupportedException as well.

2 斤斤计较的角落:我想可能有一些狡辩甚至在规定扔。该规范,在不使用preferred 必须,应该,可以空话,留下了一点打开。我读会导致多作注实现了 - 不是必须的。话又说回来,我没有看到整个的规格,因此,或许他们定义这些术语多一点或者更明确的其他地方。

2 Nitpicker's corner: I think there may be some quibble even in the "requirement" to throw. The spec, in not using the preferred "MUST, SHOULD, MAY" verbiage, leaves it a bit open. I read "causes" more as a note of implementation - not a requirement. Then again, I haven't read the entire spec, so perhaps they define these terms a bit more or are more explicit somewhere else.

这篇关于为什么在枚举类中的reset()方法必须引发NotSupportedException()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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