后端开发 \ C# \ IEnumerable1

IEnumerable1

总点击51
简介:本文导读:IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NETFramework中最基本

本文导读:IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器。它定义了一组扩展方法,用来对数据集合中的元素进行遍历、过滤、排序、搜索等操作。

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。

IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

 

一、IEnumerable、IEnumerator、ICollection、IList、List

 

IEnumerator:提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。


IEnumerable: 暴露一个IEnumerator,支持在普通集合中的遍历。


IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。


IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。

 

1、IEnumerable接口

 

C# 代码

// 摘要:

// 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。

//

// 类型参数:

// T:

// 要枚举的对象的类型。

[TypeDependency("System.SZArrayHelper")]

public interface IEnumerable<out T> : IEnumerable

{

// 摘要:

// 返回一个循环访问集合的枚举器。

//

// 返回结果:

// 可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。

IEnumerator<T> GetEnumerator();

}


可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。 

2、IEnumerator接口

 

C# 代码

public interface IEnumerator

{

bool MoveNext(); //将游标的内部位置向前移动

object Current{get;} //获取当前的项(只读属性)

void Reset(); //将游标重置到第一个成员前面

}


3、ICollection接口:ICollection<T> 同时继承IEnumerable<T>和IEnumerable两个接口

// 摘要:

// 定义操作泛型集合的方法。

//

// 类型参数:

// T:

// 集合中元素的类型。

[TypeDependency("System.SZArrayHelper")]

public interface ICollection<T> : IEnumerable<T>,IEnumerable


4、IList接口

public interface IList<T>

: ICollection<T>,IEnumerable<T>,IEnumerable

 

5、List的定义

public class List<T> : IList<T>,ICollection<T>,IList,ICollection,IEnumerable

 

 

二、IEnumerable实例

 

C# 代码

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Collections;

namespace IEnumeratorSample

{

class Person : IEnumerable

{

public string Name;//定义Person的名字

public string Age;//定义Person的年龄

public Person(string name,string age)//为Person初始化(构造函数)

{

Name = name;//配置Name值

Age = age;

}

private Person[] per;

public Person(Person[] array)//重载构造函数,迭代对象

{

per = new Person[array.Length];//创建对象

for (int i = 0; i < array.Length; i++)//遍历初始化对象

{

per[i] = array[i];//数组赋值

}

}

public IEnumerator GetEnumerator()//实现接口

{

return new PersonEnum(per);

}

}

class PersonEnum : IEnumerator//实现foreach语句内部,并派生

{

public Person[] _per;//实现数组

int position = -1;//设置“指针”

public PersonEnum(Person[] list)

{

_per = list;//实现list

}

public bool MoveNext()//实现向前移动

{

position++;//位置增加

return (position < _per.Length);//返回布尔值

}

public void Reset()//位置重置

{

position = -1;//重置指针为-1

}

public object Current//实现接口方法

{

get

{

try

{

return _per[position];//返回对象

}

catch (IndexOutOfRangeException)//捕获异常

{

throw new InvalidOperationException();//抛出异常信息

}

}

}

}

class Program

{

static void Main(string[] args)

{

Person[] per = new Person[2]

{

new Person("guojing","21"),

new Person("muqing",

};

Person personlist = new Person(per);

foreach (Person p in personlist)//遍历对象

{

Console.WriteLine("Name is " + p.Name + " and Age is " + p.Age);

}

Console.ReadKey();

}

}

}


0人推荐

推荐文章

热门标签

意见反馈 常见问题 官方微信 返回顶部