AI智能
改变未来

c#自己实现一个迭代器

直接上程序

using System;using System.Collections;using System.Collections.Generic;using System.Diagnostics;using System.Drawing;using System.IO;using System.Linq;using System.Text;using System.Threading;namespace ConsoleApp11{class Enumerator{private int[] Nums;private int Index = -1;public Enumerator(int[] nums){Nums = nums;}public  bool MoveNext(){Index++;return Index < Nums.Length;}public int Current { get { return Nums[Index]; } }}class MyList:IEnumerable{private int[] Nums { get; set; }public MyList(int n ){Nums = new int[n];var r = new Random();for (int i = 0; i < n; i++){i = r.Next(1, 10);}}public Enumerator GetEnumeratordxp(){return new Enumerator(Nums);}public IEnumerator GetEnumerator(){return GetEnumerator();}}class Program{static void Main(string[] args){var list = new MyList(5);var el = list.GetEnumeratordxp();while (el.MoveNext()){Console.WriteLine(el.Current);var el2 = list.GetEnumeratordxp();while (el2.MoveNext()){Console.WriteLine(\"\\t\" + el2.Current);}}Console.WriteLine(\"-----------------   \");Console.Read();}}}
赞(0) 打赏
未经允许不得转载:爱站程序员基地 » c#自己实现一个迭代器