Csharp/C Sharp/Class Interface/IEnumerator — различия между версиями

Материал из .Net Framework эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Версия 15:31, 26 мая 2010

Collection is iterated using the IEnumerator interface

 
using System;
using System.Collections;
public class Starter {
    public static void Main() {
        SimpleCollection simple = new SimpleCollection(new object[] { 1, 2, 3, 4, 5, 6, 7 });
        IEnumerator enumerator = simple.GetEnumerator();
        while (enumerator.MoveNext()) {
            Console.WriteLine(enumerator.Current);
        }
    }
}
public class SimpleCollection : IEnumerable {
    public SimpleCollection(object[] array) {
        items = array;
    }
    public IEnumerator GetEnumerator() {
        return new Enumerator(items);
    }
    private object[] items = null;
}
public class Enumerator : IEnumerator {
    public Enumerator(object[] items) {
        elements = new object[items.Length];
        Array.Copy(items, elements, items.Length);
        cursor = -1;
    }
    public bool MoveNext() {
        ++cursor;
        if (cursor > (elements.Length - 1)) {
            return false;
        }
        return true;
    }
    public void Reset() {
        cursor = -1;
    }
    public object Current {
        get {
            if (cursor > (elements.Length - 1)) {
                throw new InvalidOperationException("Enumeration already finished");
            }
            if (cursor == -1) {
                throw new InvalidOperationException(
                    "Enumeration not started");
            }
            return elements[cursor];
        }
    }
    private int cursor;
    private object[] elements = null;
}


IEnumerator Example (Static Collection)

 
using System;
using System.Collections;
public class SimpleCollection : IEnumerable {
    public SimpleCollection(object[] array) {
        items = array;
    }
    public IEnumerator GetEnumerator() {
        return new Enumerator(this);
    }
    private class Enumerator : IEnumerator {
        public Enumerator(SimpleCollection obj) {
            oThis = obj;
            cursor = -1;
        }
        public bool MoveNext() {
            ++cursor;
            if (cursor > (oThis.items.Length - 1)) {
                return false;
            }
            return true;
        }

        public void Reset() {
            cursor = -1;
        }
        public object Current {
            get {
                if (cursor > (oThis.items.Length - 1)) {
                    throw new InvalidOperationException(
                        "Enumeration already finished");
                }
                if (cursor == -1) {
                    throw new InvalidOperationException(
                        "Enumeration not started");
                }
                return oThis.items[cursor];
            }
        }
        private int cursor;
        private SimpleCollection oThis;
    }

    private object[] items = null;
}


iterates two collections simultaneously

 
using System;
using System.Collections.Generic;
public class Starter {
    public static void Main() {
        MyClass obj = new MyClass();
        foreach (int item in obj) {
            Console.Write(item);
        }
    }
}
public class MyClass {
    private int[] list1 = new int[] { 0, 2, 4, 6, 8 };
    private int[] list2 = new int[] { 1, 3, 5, 7, 9 };
    public IEnumerator<int> GetEnumerator() {
        for (int index = 0; index < 4; ++index) {
            yield return list1[index];
            yield return list2[index];
        }
    }
}


yield IEnumerator

 

using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

public class Primes {
    private long min;
    private long max;
    public Primes(long minimum, long maximum) {
        min = minimum;
        max = maximum;
    }
    public IEnumerator GetEnumerator() {
        for (long possiblePrime = min; possiblePrime <= max; possiblePrime++) {
            bool isPrime = true;
            for (long possibleFactor = 2; possibleFactor <= (long)Math.Floor(Math.Sqrt(possiblePrime)); possibleFactor++) {
                long remainderAfterDivision = possiblePrime % possibleFactor;
                if (remainderAfterDivision == 0) {
                    isPrime = false;
                    break;
                }
            }
            if (isPrime) {
                yield return possiblePrime;
            }
        }
    }
}
class Program {
    static void Main(string[] args) {
        Primes primesFrom2To1000 = new Primes(2, 1000);
        foreach (long i in primesFrom2To1000)
            Console.Write("{0} ", i);
    }
}