C#中IEnumerable接口介绍并实现自定义集合

Faith ·
更新时间:2024-09-20
· 1143 次阅读

简介

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。对于所有数组的遍历,都来自IEnumerable接口。
IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

一、foreach在IEnumerable中案例 public static void Test3() { MyInt temp = new MyInt(); foreach (int item in temp) Console.WriteLine(item); } //foreach的必须要实现IEnumerable和IEnumerator的接口 public class MyInt : IEnumerable { int[] temp = { 1, 32, 43, 343 }; public IEnumerator GetEnumerator() { return temp.GetEnumerator(); } }

相当于下面代码:

public static void Test1() { int[] myArray = { 1, 32, 43, 343 }; //获取要遍历的枚举数 IEnumerator myie = myArray.GetEnumerator(); //重置当前项,相当于把指针移到初始位置:position = -1; 一开始认识数组的索引从“0”开始 myie.Reset(); //向前移动一个索引,返回Bool类型,判断是否超出下标 while (myie.MoveNext()) { int i = (int)myie.Current;//从Object转成对应类型 Console.WriteLine("Value: {0}", i); } }

包含一个属性两个方法
MoveNext:把当前的项移动到下一项(类似于索引值),返回一个bool值,这个bool值用来检查当前项是否超出了枚举数的范围!
Current:获取当前项的值,返回一个object的类型!
Reset:顾名思义也就是把一些值恢复为默认值,比如把当前项恢复到默认状态值!

二、Lamda在IEnumerable中案例 //lamda表达式在数组中查询 public static void Test2() { List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" }; //List<string> query = fruits.Where(fruit => fruit.Length < 6).ToList(); IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6); foreach (string fruit in query) Console.WriteLine(fruit); }

只筛选出List中的元素长度小于6的值,然后打印出。

实现自定义集合的 IEnumerable和IEnumerator 接口 namespace ConsoleApplication1 { //定义Person类 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 { private Person[] _people; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)GetEnumerator(); } //获取枚举数 public PeopleEnum GetEnumerator() { return new PeopleEnum(_people); } } public class PeopleEnum : IEnumerator { public Person[] _people; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public PeopleEnum(Person[] list) { _people = list; } //向下推移索引,返回Bool类型值 public bool MoveNext() { position++; return (position < _people.Length); } //重置默认索引位置,默认下标为0 public void Reset() { position = -1; } object IEnumerator.Current { get { return Current; } } //当前索引值 public Person Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } class Program { static void Main(string[] args) { //实例化Person 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); } } }

到此这篇关于C#中IEnumerable接口并实现自定义集合的文章就介绍到这了。希望对大家的学习有所帮助,也希望大家多多支持软件开发网。



自定义 C# ienumerable

需要 登录 后方可回复, 如果你还没有账号请 注册新账号