zoukankan      html  css  js  c++  java
  • 手动书写小代码-foreach实现机制

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace 枚举的实现机制
    {
        class Program
        {
            static void Main(string[] args)
            {
                int[] arr = { 1,2,3,4,6};
                IEnumerator ie = arr.GetEnumerator();
                while(ie.MoveNext()==true)
                {
                    int i = (int)ie.Current;
                    Console.WriteLine(i);
                }
                Console.ReadLine();
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Collections;
    
    namespace 详细实现机制
    {
        class ColorEnumerator:IEnumerator//实现IEnumerator接口
        {
            string[] Colors;
            int Position = -1;
            //实现Current方法
            public object Current
            {
                get
                { 
                    if(Position==-1)
                    {
                        throw new InvalidOperationException();//当前状态无效是引发的异常
                    }
                    else 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 < theColors.Length;i++ )
                {
                    Colors[i]=theColors[i];
                    Console.WriteLine(Colors[i]);
                }
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 详细实现机制
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] arr = { "Red", "Blue", "Yellow", "Green", "Gray", "Orange" };
                ColorEnumerator color = new ColorEnumerator(arr);
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    2015年 Stoi&Gdoi 反思总结与未来计划
    bzoj4517: [Sdoi2016]排列计数--数学+拓展欧几里得
    bzoj4518: [Sdoi2016]征途--斜率DP
    BZOJ 1391: [Ceoi2008]order
    BZOJ 2527: [Poi2011]Meteors
    BZOJ 2087: [Poi2010]Sheep
    BZOJ 1283: 序列
    BZOJ 1914: [Usaco2010 OPen]Triangle Counting 数三角形
    BZOJ 3513: [MUTC2013]idiots
    BZOJ 3771: Triple
  • 原文地址:https://www.cnblogs.com/chenyongblog/p/3175781.html
Copyright © 2011-2022 走看看