IEnumerable和IEnumerator在同一个类中,不好的主意吗? [英] IEnumerable and IEnumerator in the same class, bad idea?

查看:60
本文介绍了IEnumerable和IEnumerator在同一个类中,不好的主意吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是个坏主意吗?

Private Class GH_DataStructureEnumerator(Of Q As Types.IGH_Goo)
  Implements IEnumerable(Of Q)
  Implements IEnumerator(Of Q)
  ....
  ....
  'Current, MoveNext, Reset etc.'
  ....
  ....

  Public Function GetEnumerator_Generic() As IEnumerator(Of Q) _
                  Implements IEnumerable(Of Q).GetEnumerator
    Return Me
  End Function
End Class

此类仅作为IEnumerable(Of T)只读属性可见,它为我节省了一个包装IEnumerator(Of T)的附加类.但是不知何故,这似乎是错误的.有更好的方法吗?

This class is only visible as an IEnumerable(Of T) readonly property, and it saves me an additional class that wraps IEnumerator(Of T). But somehow it just seems wrong. Is there a better way?

推荐答案

这不是一个好主意,因为如果有人尝试同时枚举您的收藏集,则解决方案将失效.

This is a bad idea because the solution will break down if someone attempts to enumerate your collection more than once at the same time.

例如:这将以某种方式破坏

For example: This will break in some way

For Each cur1 in yourObj 
  For Each cur2 in yourObj
    Console.WriteLine("{0} - {1}", cur1,cur2)
  Next
Next

这篇关于IEnumerable和IEnumerator在同一个类中,不好的主意吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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