using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ConsoleApplication46
{
    class Program
    {
        // IEnumerator  接口里面有三个函数成员 : Current ,MoveNext,Rest ; MoveNext 必须在第一次使用Current之前使用
        // IEnumerable   接口里面只有一个成员  GetEnumerator 方法 返回的事 对象的枚举数 -- IEnumerator 
        static void Main(string[] args)
        {
            int[] a = new int[] { 1, 2, 5, 7 };
            foreach (var item in a)
            {
                Console.WriteLine(item);
            }
            Mycolrs mc = new Mycolrs();
            foreach (string  item in mc)
            {
                Console.WriteLine(item);
            }
            Console.ReadKey();
        }
    }
    class ColorEnumerator : IEnumerator
    {
        int Position = -1;
        string[] Colors;
        public object Current
        {
            get
            {
                if (Position == -1)
                {
                    throw new InvalidOperationException();
                }
                if (Position == Colors.Length)
                {
                    throw new InvalidOperationException();
                }
                return Colors[Position];
            }
        }
        public bool MoveNext()
        {
            if (Position < Colors.Length - 1)
            {
                Position++;
                return true;
            }
            else
            {
                return false;
            }
        }
        public void Reset()
        {
            Position = -1;
        }
        public ColorEnumerator(string[] theColors)
        {
            Colors = new string[theColors.Length];
            for (int i = 0; i < Colors.Length; i++)
            {
                Colors[i] = theColors[i];
            }
        }
    }
   class Mycolrs:IEnumerable
   {
       string[] Colors = { "red", "Yellow", "Blue" };
       public IEnumerator GetEnumerator()
       {
          return  new ColorEnumerator(Colors);
       }
   }
}