如何为多个foreach实现正确的IEnumerator接口?

时间:2011-10-05 08:14:04

标签: c# .net ienumerable ienumerator

我有一个代码:

        class T : IEnumerable, IEnumerator
        {
            private int position = -1;

            public T() { }

            public IEnumerator GetEnumerator() { return this; }

            public object Current { get { return position; } }

            public bool MoveNext()
            {
                position++;
                return (position < 5);
            }

            public void Reset() { position = -1; }
        }

//Using in code:
T t = new T();
foreach (int i in t)
 //to do something

在上面的代码中,一切正常,但是当我使用下一个时:

foreach (int i in t)
   if (i == 2)
     foreach (int p in t)
       //print p
   else
       //print i

打印(括号内的第二个循环):0 1(3 4)2而不是0 1(0 1 2 3 4)2 3 4 我在List和Collection上测试了它,他们做得对。 我怎样才能得到我需要的东西?

2 个答案:

答案 0 :(得分:9)

你不能,因为你已经使你的代码表面成为一个枚举器,本身就是IMO的错误。对我来说,更好的版本是:

class T : IEnumerable<int> {
    public IEnumerator<int> GetEnumerator() {
        int i = 0;
        while(i < 5) {
            yield return i;
            i++;
        }
    }
    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
}

编译器将使用单独的枚举器创建正确的设备。

除非您是为.NET 1.1编写的,否则如果您发现自己手动编写了一个enumarator,那么很有可能您正在以艰难的方式执行此操作,并将其作为奖励错误

如果你 真的必须 那么难以做到:

class T : IEnumerable<int>
{
    public T() { }

    public IEnumerator<int> GetEnumerator() { return new TEnumerator(); }
    IEnumerator IEnumerable.GetEnumerator() { return GetEnumerator(); }
    private class TEnumerator : IEnumerator<int>
    {
        private int position = -1;
        public int Current { get { return position; } }
        object IEnumerator.Current { get { return Current; } }
        void IDisposable.Dispose() {}
        public bool MoveNext()
        {
            position++;
            return (position < 5);
        }
        public void Reset() { position = -1; }
    } 
}

此处的重要性是TEnumerator的不同实例允许相同的 T实例单独迭代。

答案 1 :(得分:5)

foreach (int i in t)
   if (i == 2)
     foreach (int p in t)
       //print p
   else
       //print i

首先始终使用大括号,而缩进匹配将会发生的另一个if将导致混淆。

foreach (int i in t) {
   if (i == 2) {
     foreach (int p in t) {
       //print p
      }
   } else {
       //print i
   }
 }

但问题是:每个T实例只有一个计数器,并且您使用的是同一个实例。因此,你做了一次。如果要允许并发枚举,则枚举器对象需要与GetEnumerator分开,每次都返回一个新实例。