e心e意

导航

C#枚举数和迭代器

声明IEnumerable的枚举数

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//漏掉就会出错

namespace 枚举数IEnumerator
{
    /*IEnumerator接口包含3个函数成员:Current,MoveNext以及Reset
     Current返回序列中当前置项的属性
     他是只读属性。
     他返回object类型的引用,所以可以返回任何类型。
     MoveNext是把枚举数位置前进到集合中下一项的方法。
     他也返回布尔值,知识新的位置是有效位置或
     已经超过了序列的尾部。
     如果新位置是有效的,方法返回true。
     反之,返回false;
     枚举数的原始位置在序列中的第一项之前。MoveNext必须
     在第一次使用Current之前使用。
     Reset方法把位置重置为原始状态。*/
    class Program
    {
        static void Main(string[] args)
        {
            int[] MyArray = { 10,11,12,13};//创建数组
            IEnumerator ie = MyArray.GetEnumerator();//获取枚举数
            while (ie.MoveNext())//移到下一项
            {
                int i = (int)ie.Current;//获取当前项
                Console.WriteLine("0",i);//输出
            }
        }
    }
}

IEnumerable接口2

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//一定要写上

namespace 声明IEnumerator的枚举数2
{
    class ColorEnumerator : IEnumerator
    {
        string[] Colors;
        int Position = -1;
        public object Current//Current方法
        {
            get
            {
                if (Position == -1)
                    throw new InvalidOperationException();
                if (Position == Colors.Length )
                    throw new InvalidOperationException();
                return Colors[Position];
            }
        }
        public bool MoveNext()//MoveNext方法
        {
            if (Position < Colors.Length - 1)
            {
                Position++;
                return true;
            }
            else
                return false;
        }
        public void Reset()//Reset方法
        {
            Position = -1;
        }
        public ColorEnumerator(string[] theColors)//
        {
            Colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
                Colors[i] = theColors[i];
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
        }
    }
}

3.IEnumerator和IEnumerable的示例

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;//必须添上

namespace IEnumerator和IEnumerable的示例
{
    class ColorEnumerator : IEnumerator
    {
        string[] Colors;
        int Position = -1;
        public ColorEnumerator(string[] theColors)//构造函数
        { 
        Colors =new string[theColors .Length ];
        for (int i = 0; i < theColors.Length; i++)
            Colors[i] = theColors[i];
        }
        public object Current//Current方法
        {
            get
            {
                if (Position == -1)
                {
                    throw new InvalidOperationException();
                }
                if (Position == Colors.Length)
                {
                    throw new InvalidOperationException();
                }
                return Colors[Position];
            }
        }
        public bool MoveNext()//MoveNext方法
        {
            if (Position < Colors.Length - 1)
            {
                Position++;
                return true;
            }
            else
                return false;
        }
        public void Reset()//Reset方法
        { Position = -1; }
    }
    /*IEnumerable接口只有一个成员——GetEnumerator方法,
     它返回对象的枚举数。*/
    class MyColors : IEnumerable
    {
        string[] Colors = { "Red","Yellow","Blue"};
        public IEnumerator GetEnumerator()//GetEnumerator方法
        {
            return new ColorEnumerator(Colors );
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyColors mc = new MyColors();
            foreach (string color in mc)
                Console.WriteLine(color );
        }
    }
}

4.使用迭代器来创建枚举数

namespace 使用迭代器来创建枚举数
{
    /*迭代器可以把手动编码的可枚举类型和枚举数
     替换为由迭代器生成的可枚举类型和枚举数。
     迭代器是有一个和多个yield语句的代码块。
     有两个特殊语句:
     yield return语句执行了序列中返回的下一项。
     yield break语句制定在序列中没有更多项。*/
    class MyClass
    { 
    public IEnumerator<string>GetEnumetator()
    {
        return BlackAndWhite();//返回枚举数
    }
        public IEnumerator <string>BlackAndWhite()//迭代器
        {
            yield return "black";
            yield return "gray";
            yield return "white";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            foreach (string shade in mc)
                Console.WriteLine(shade);
        }
    }
}

5.使用迭代器来创建可枚举类型

namespace 使用迭代器创建可枚举类型
{
    class MyClass
    {
        public IEnumerator<string> GetEnumerator()
        {
            IEnumerable<string> MyEnumerable = BlackAndWhite();//获取枚举数
            return MyEnumerable.GetEnumerator();//获取枚举数
        }
        public IEnumerable <string>BlackAndWhite()//迭代器
        {
            yield return "black";
            yield return "gray";
            yield return "white";
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc = new MyClass();
            foreach (string shade in mc)
                Console.WriteLine("{0}",shade );
            foreach (string shade in mc.BlackAndWhite())//使用类枚举数方法
                Console.Write("{0}",shade );
            Console.ReadLine();
        }
    }
}

6.产生多个可枚举类型

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.Generic;//需要此命名空间

namespace 产生多个可枚举类型
{
    /*ColorCollection类有两个可枚举类型的迭代器,
     一个正序进行枚举,一个逆序进行枚举;*/
    class ColorCollection
    {
        string[] Colors = { "Red","Orange","Yellow","Green","Blue","Purple"};
        public IEnumerable<string> Forward()//可枚举类型迭代器
        { 
        for(int i=0;i<Colors .Length ;i++)
            yield return Colors [i];
        }
        public IEnumerable<string> Reverse()//可枚举类型迭代器
        { 
        for(int i=Colors .Length -1;i>=0;i--)
            yield return Colors [i];
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            ColorCollection cc = new ColorCollection();
            foreach (string color in cc.Forward())
                Console.Write("{0}",color );
            Console.WriteLine();

            foreach (string color in cc.Reverse())
                Console.Write("{0}",color );
            Console.WriteLine();

            IEnumerable<string> ieable = cc.Reverse();
            IEnumerator<string> iearor = ieable.GetEnumerator();

            while (iearor.MoveNext())
                Console.WriteLine("{0}",iearor .Current );
            Console.WriteLine();
        }
    }
}

7.产生多个枚举数

namespace 产生多个枚举数
{
    /*本实例演示两个方面的内容:
     第一,它使用迭代器产生具有两个枚举数的类。
     第二,他演示了迭代器如何能实现为属性而不是方法。
   枚举器:在C#中,一个对象只要实现
     * System.Collections.IEnumerator或者
     * System.Collections.Generic.IEnumerator<T>
     * (继承自IEnumerator)接口,
     * 那么这个对象就是一个枚举器。

可枚举对象:是一个实现了
     * System.Collections.IEnumerable或
     * System.Collections.Generic.IEnumerable<T>
     * (继承自IEnumerable)的对象。

迭代器就是用于生成
     * 一个既是枚举器又是可枚举对象的。

     * */
    class MyClass : IEnumerable<string>
    {
        bool ColorFlag = true;
        public MyClass(bool flag)//Costructor
        {
            ColorFlag = flag;
        }
        IEnumerator<string> BlackAndWhite//属性——枚举数迭代器
        {
            get
            {
                yield return "black";
                yield return "gray";
                yield return "white";
            }
        }
        IEnumerator<string> Colors//属性——枚举数迭代器
        {
            get
            {
                string[] theColors = { "blue","red","yellow"};
                for(int i=0;i<theColors .Length ;i++)
                    yield return theColors [i];
            }
        }
        public IEnumerator<string> GetEnumerator()//GetEnumerator方法
        {
            return ColorFlag
                ? Colors //返回Colors的枚举数
                : BlackAndWhite;//返回BlackAndWhite的枚举数
        }
        System.Collections.IEnumerator
            System.Collections.IEnumerable.GetEnumerator()
        { 
        return  ColorFlag 
            ?Colors
            :BlackAndWhite ;
        }
    }
    class Program
    {
        static void Main(string[] args)
        {
            MyClass mc1=new MyClass (true);
            foreach (string s in mc1 )
                Console .Write ("{0}",s );
            Console .WriteLine ();

            MyClass mc2=new MyClass (false );
            foreach (string s in mc2)
                Console .Write ("{0}",s );
            Console .WriteLine ();
        }
    }
}

posted on 2014-05-31 19:17  e心e意  阅读(127)  评论(0)    收藏  举报