如果一个GetEnumerator方法还在上课的时候​​不实现IEnumerable幂等 [英] Should a GetEnumerator method still be idempotent when the class does not implement IEnumerable

查看:155
本文介绍了如果一个GetEnumerator方法还在上课的时候​​不实现IEnumerable幂等的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此问题的另一个小猪背上<一href="http://stackoverflow.com/questions/4189581/should-an-ienumerable-iterator-on-a-queue-dequeue-an-item">question这是我提出的关于修改一个对象,你迭代它滥用IEnumerable接口。

This question piggy backs of another question which I raised regarding abusing the IEnumerable interface by modifying an object as you iterate over it.

普遍的共识是,没有任何实现IEnumerable应该幂等。但是.NET支持编译时鸭打字foreach语句。提供一个IEnumerator的GetEnumerator()方法的任何对象可以使用一个foreach语句内

The general consensus is that no anything that Implements IEnumerable should be idempotent. But .net supports compile time duck typing with the foreach statement. Any object that provides an IEnumerator GetEnumerator() method can be used inside a foreach statement.

所以应该GetEnumerator方法幂等或者是它时,它实现IEnumerable

So should the GetEnumerator method be idempotent or is it when it implements IEnumerable?

EDIT(由上下文)

EDIT (Added context)

要放一些背景下轮这就是我的建议是,遍历队列时,每个项目都出列,因为它去。此外,该调用的GetEnumerator后推到队列中的任何新对象仍然会遍历。

To put some context round this what I am suggesting is that when iterating over a queue each item is dequeued as it goes. Additionally any new objects pushed onto the queue after the call to GetEnumerator would still be iterated over.

推荐答案

这不是键入的是幂等 - 这甚至没有多大意义;你的可以的意思是不变的,但是这并不清楚。这是的GetEnumerator 方法本身通常是幂等。

It's not the type which is idempotent - that doesn't even make much sense; you may mean immutable, but that's not clear. It's the GetEnumerator method itself which is typically idempotent.

虽然我会说,是的一般的情况下,我可以预见的特殊情况下,是有意义的具有非幂等的GetEnumerator 方法。例如,这可能是因为你有这只能读一次(因为它是从一个Web服务器,它不会再次服务于同样的请求,或者类似的东西流)的数据。在这种情况下,的GetEnumerator 必须有效无效的数据源,从而使未来的调用会抛出异常。

While I'd say that's typically the case, I can envisage special cases where it makes sense to have a non-idempotent GetEnumerator method. For example, it could be that you've got data which can only be read once (because it's streaming from a web server which won't service the same request again, or something like that). In that case, GetEnumerator would have to effectively invalidate the data source, so that future calls would throw an exception.

这样的类型和方法应记录非常谨慎,当然,但我认为他们是合理的。

Such types and methods should be documented very carefully, of course, but I think they're reasonable.

这篇关于如果一个GetEnumerator方法还在上课的时候​​不实现IEnumerable幂等的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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