Csharp/C Sharp by API/System.Collections/IEnumerator

Материал из .Net Framework эксперт
Версия от 12:08, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

IEnumerator.Current

  
using System;
using System.Collections;
class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    
    string[] anotherStringArray = {"Here"s", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    
  }
}


IEnumerator.MoveNext()

  
using System;
using System.Collections;
class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    
    string[] anotherStringArray = {"Here"s", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    
  }
}


IEnumerator.Reset()

  
using System;
using System.Collections;
class MainClass
{
  public static void Main()
  {
    ArrayList myArrayList = new ArrayList();
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    myArrayList.Add("A");
    
    string[] anotherStringArray = {"Here"s", "some", "more", "text"};
    myArrayList.SetRange(0, anotherStringArray);
    Console.WriteLine("Using the GetEnumerator() method to get an enumerator");
    IEnumerator myEnumerator = myArrayList.GetEnumerator();
    while (myEnumerator.MoveNext())
    {
      Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    }
    myEnumerator.Reset();
    myEnumerator.MoveNext();
    Console.WriteLine("myEnumerator.Current = " + myEnumerator.Current);
    
  }
}


Implement IEnumerator

  
using System;
using System.Threading;
using System.Collections;
using System.Collections.Generic;
public class MyEnumerable<T> : IEnumerable<T>
{
    public MyEnumerable( T[] items ) {
        this.items = items;
    }
    public IEnumerator<T> GetEnumerator() {
        return new NestedEnumerator( this );
    }
    IEnumerator IEnumerable.GetEnumerator() {
        return GetEnumerator();
    }
    // The enumerator definition.
    class NestedEnumerator : IEnumerator<T>
    {
        public NestedEnumerator( MyEnumerable<T> coll ) {
            Monitor.Enter( coll.items.SyncRoot );
            this.index = -1;
            this.coll = coll;
        }
        public T Current {
            get { return current; }
        }
        object IEnumerator.Current {
            get { return Current; }
        }
        public bool MoveNext() {
            if( ++index >= coll.items.Length ) {
                return false;
            } else {
                current = coll.items[index];
                return true;
            }
        }
        public void Reset() {
            current = default(T);
            index = 0;
        }
        public void Dispose() {
            try {
                current = default(T);
                index = coll.items.Length;
            }
            finally {
                Monitor.Exit( coll.items.SyncRoot );
            }
        }
        private MyEnumerable<T> coll;
        private T current;
        private int index;
    }
    private T[] items;
}
public class EntryPoint
{
    static void Main() {
        MyEnumerable<int> integers = new MyEnumerable<int>( new int[] {1, 2, 3, 4} );
        foreach( int n in integers ) {
            Console.WriteLine( n );
        }
    }
}