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