C#中的枚举使用泛型接口和非泛型接口实现

        枚举相信大家都不陌生,今天又再看了下感觉有了新的收获。下面记录自己的代码:

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumberOption
{
    /// <summary>
    /// 声明一个枚举类
    /// </summary>
    public class MyIEnumberator
    {
        /// <summary>
        /// 迭代器返回枚举数
        /// </summary>
        public MyYield mcyield=new MyYield();
    }

    /// <summary>
    /// 非泛型接口枚举数
    /// </summary>
    public class ColorEnumberator : IEnumerator
    {
        string[] Colors;//实现IEnumberator
        /// <summary>
        /// 索引位置
        /// </summary>
        int Postion = -1;
        /// <summary>
        /// 当前元素
        /// </summary>
        public object Current
        {
            get
            {
                if (Postion == -1)
                {
                    throw new InvalidOperationException();
                }
                if (Postion == Colors.Length)
                {
                    throw new InvalidOperationException();
                }
                return Colors[Postion];
            }
        }

        /// <summary>
        /// 移动到下一个元素
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if (Postion < Colors.Length - 1)
            {
                Postion++;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            Postion = -1;
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="theColors"></param>
        public ColorEnumberator(string[] theColors)
        {
            Colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
            {
                Colors[i] = theColors[i];
            }
        }
    }

    /// <summary>
    /// 泛型枚举数接口实现ColorEnumerator
    /// </summary>
    public class ColorEnumberatorList : IEnumerator<String>
    {
        string[] Colors;
        int Position = -1;
        /// <summary>
        /// 当前元素 泛型
        /// </summary>
        public string Current
        {
            get
            {
                return Colors[Position];
            }
        }

        /// <summary>
        /// 显示实现Current 非泛型
        /// </summary>
        object IEnumerator.Current
        {
            get { return Colors[Position];}
        }

        /// <summary>
        /// 移动到下一个
        /// </summary>
        /// <returns></returns>
        public bool MoveNext()
        {
            if (Position < Colors.Length - 1)
            {
                Position++;
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 重置
        /// </summary>
        public void Reset()
        {
            Position = -1;
        }

        /// <summary>
        /// 销毁操作
        /// </summary>
        public void Dispose()
        {
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="theColors"></param>
        public ColorEnumberatorList(string[] theColors)
        {
            Colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
            {
                Colors[i] = theColors[i];
            }
        }
    }
}

using EnumberOption;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageBookCode.Enumerator
{
    /// <summary>
    /// 非泛型实现
    /// </summary>
    public class MyColors:IEnumerable
    {
        string[] Colors ;

        /// <summary>
        /// ColorEnumberator实现了IEnumerator
        /// </summary>
        /// <returns></returns>
        public IEnumerator GetEnumerator()
        {
            return new ColorEnumberator(Colors);
        }

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="theColors"></param>
        public MyColors(string[] theColors)
        {
            Colors = new string[theColors.Length];
            for (int i = 0; i < theColors.Length; i++)
            {
                Colors[i] = theColors[i];
            }
        }

        /// <summary>
        /// 无参构造函数
        /// </summary>
        public MyColors()
        {
            Colors = new string[3];
            Colors[0] = "Red";
            Colors[1] = "Yellow";
            Colors[2] = "Blue";
        }
    }

    /// <summary>
    /// 泛型实现
    /// </summary>
    public class MyColorsList : IEnumerable<string>
    {
        string[] Colors = { "Red", "Yellow", "Blue" };
        /// <summary>
        /// IEnumerable<T>版本"
        /// </summary>
        /// <returns></returns>
        public IEnumerator<string> GetEnumerator()
        {
            return new ColorEnumberatorList(Colors);
        }

        /// <summary>
        /// 显示实现 IEnumerable版本
        /// </summary>
        /// <returns></returns>
        IEnumerator IEnumerable.GetEnumerator()
        {
            return new ColorEnumberatorList(Colors);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EnumberOption
{
    /// <summary>
    /// 使用迭代器
    /// </summary>
    public class MyYield
    {
        /// <summary>
        /// 返回枚举数
        /// </summary>
        /// <returns></returns>
        public IEnumerator<string> GetEnumerator()
        {
            返回枚举数
            return BlackAndWhite();
        }

        /// <summary>
        /// 迭代器
        /// </summary>
        /// <returns></returns>
        public IEnumerator<string> BlackAndWhite()
        {
            yield return "black";
            yield return "gray";
            yield return "white";
        }
    }
}

using EnumberOption;
using ImageBookCode.Enumerator;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ImageBookCode
{
    class Program
    {
        static void Main(string[] args)
        {
            实现接口的枚举数
            MyColors mc = new MyColors();
            foreach (var item in mc)
            {
                Console.WriteLine(item);
            }
            使用泛型接口实现的枚举数
            MyColorsList mcl = new MyColorsList();
            foreach (var color in mcl)
            {
                Console.WriteLine(color);
            }
            使用迭代器返回枚举数
            MyIEnumberator IEnumberColorYield = new MyIEnumberator();
            foreach (var itemIEnum in IEnumberColorYield.mcyield)
            {
                Console.WriteLine(itemIEnum);
            }
            Console.ReadKey();
        }
    }
}

代码打包下载点击这里 开发环境:VS2013

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值