码迷,mamicode.com
首页 > 编程语言 > 详细

枚举 IEnumerable

时间:2016-12-15 11:38:22      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:循环   dex   ble   ring   bsp   next   range   返回   namespace   

namespace _03
{
class Program
{

public static void Main()
{
Person person = new Person();
IEnumerator ienu= person.GetEnumerator();
while (ienu.MoveNext())
{
Console.WriteLine(ienu.Current);
}
}
}

 

class Person :IEnumerable // 返回一个循环访问集合的枚举数
{
private string[] _names = { "夹心饼干","大师","香肠"};
private string _email;

private int _age;
public int Age
{
get { return _age; }
set { _age = value; }
}
public string Email
{
get
{ return _email; }
set
{ _email = value; }
}
public string[] Names
{
get { return _names; }
set { _names = value; }
}
private string _name;
public string Name
{
get { return _name; }
set { _name = value; }
}
// 一个可用于循环访问集合的 System.Collections.IEnumerator 对象。

public IEnumerator GetEnumerator()
{
return new Person1(Names);
}
}
class Person1:IEnumerator
{
public Person1(string[] names)
{
_names = names;
}
public Person1() { }
private string[] _names;
public string[] Names
{
get { return _names; }
set { _names = value; }
}
private int index = -1;
public int Index
{
get { return index; }
set { index = value; }
}
public string this[int index]
{
get { return this[index]; }
}
public object Current
{
get
{
if(Index<Names.Length&&Index>=0)
{
return Names[Index];
}
else
{
throw new IndexOutOfRangeException();
}
}

}

public bool MoveNext()
{
if (index+1<Names.Length)
{
index++;
return true;

}
return false;
}
public void Reset()
{
index = -1;
}
}


}

枚举 IEnumerable

标签:循环   dex   ble   ring   bsp   next   range   返回   namespace   

原文地址:http://www.cnblogs.com/wrnsweet/p/6182294.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!