手动书写小代码-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 = { ,,,,};
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 = -;
//实现Current方法
public object Current
{
get
{
if(Position==-)
{
throw new InvalidOperationException();//当前状态无效是引发的异常
}
else if(Position==Colors.Length)
{
throw new InvalidOperationException();
}
return Colors[Position];
}
}
public bool MoveNext()
{
if (Position < Colors.Length - )
{
Position++;
return true;
}
else
{
return false;
}
}
public void Reset()
{
Position = -;
}
public ColorEnumerator(string[]theColors)
{
Colors=new string[theColors.Length];
for (int i = ; 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();
}
}
}
上一篇:关于CSS动画效果的图片展示


下一篇:VB读写INI文件的四个函数以及相关API详细说明