您的位置:首页 > 编程语言 > C#

C#接口之IEnumerable,IEnumerator

2015-10-19 14:59 831 查看
IEnumerable



截图来源于https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerable.getenumerator.aspx

IEnumerable只包含一个抽象方法GetEnumerable(),它返回一个可用于循环访问集合的IEnumberator对象。我们可以通过定义foreach语句功能实现并支持非泛型方法的迭代。

在MSDN上给出了一个关于GetEnumerable()方法的例子:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace TestInterface
{

public class Person
{
public Person(string fName,string lName)
{
this.firstName = fName;
this.lastName = lName;
}

public string firstName;
public string lastName;
}

public class People:IEnumerable//继承IEnumerable类,重写GetNumberable()方法
{
private Person[] _peopele;
public People(Person[] pArray)
{
_peopele = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
_peopele[i] = pArray[i];
}

IEnumerator IEnumerable.GetEnumerator()//实现GetEnumerable接口
{
return (IEnumerator) GetEnumerator();//调用重写的GetEnumerator方法
}

public PeopleEnum GetEnumerator()
{
return new PeopleEnum(_peopele);//返回一个PeopleEnum类型的对象,PeopleEnum继承了IEnumerator类
}
}

public class PeopleEnum:IEnumerator
{
public Person[] _people;
int position = -1;
/// <summary>
/// 构造函数
/// </summary>
/// <param name="list">
/// _people指针指向list数组
/// </param>
public PeopleEnum(Person[] list)
{
_people = list;
}

/// <summary>
///将枚举数推进到集合的下一个元素。         /// </summary>
/// <returns>
/// 成功返回true,失败返回false
/// </returns>
public bool MoveNext()
{
position++;
return (position < _people.Length);
}

/// <summary>
/// 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
/// </summary>
public void Reset()
{
position = -1;
}

/// <summary>
/// 获取当前位置的对象
/// </summary>
object IEnumerator.Current
{
get { return Current; }
}

/// <summary>
/// 实现接口方法
/// </summary>
public Person Current
{
get
{
try
{
return _people[position];
}
catch(IndexOutOfRangeException)
{
throw new InvalidOperationException();
}
}
}
}

class Program
{

static void Main(string[] args)
{
Person[] peopleArray = new Person[3]
{
new Person("John","Smith"),
new Person("Jim","Johnson"),
new Person("Sue","Rabon"),
};
People peopleList = new People(peopleArray);//初始化对象集合
foreach (Person p in peopleList)
Console.WriteLine(p.firstName + " " + p.lastName);

}
}
}


如果我们将第23-42代码改写如下:

public class People//继承IEnumerable类,重写GetNumberable()方法
{
private Person[] _peopele;
public People(Person[] pArray)
{
_peopele = new Person[pArray.Length];
for (int i = 0; i < pArray.Length; i++)
_peopele[i] = pArray[i];
}
}


即不让People继承IEnumerable类,重写GetNumerator接口的部分也删除掉。那么编译器就会提示:



这是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,以至于foreach语句不能遍历Person集合。

IEnumerator

下面来介绍一下IEnumerator



截图来源于:https://msdn.microsoft.com/zh-cn/library/system.collections.ienumerator(v=vs.110).aspx

属性 描述

Current 获取集合中的当前元素

方法 描述

MoveNext 将枚举数推进到集合的下一个元素。

Reset 将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。

正是因为People类中没有实现GetNumerator()方法,所以People对象就不能返回一个IEnumerator对象,那么也就不能使用MoveNext方法,foreach也就不能实现遍历Person集合。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: