时间:2021-05-20
本文主要记录我在学习C#中foreach遍历原理的心得体会。
对集合中的要素进行遍历是所有编码中经常涉及到的操作,因此大部分编程语言都把此过程写进了语法中,比如C#中的foreach。经常会看到下面的遍历代码:
实际此代码的执行过程:
会发现有GetEnumerator()方法和IEnumerator<string>类型,这就涉及到可枚举类型和枚举器的概念。
为了方便理解,以下为非泛型示例:
实现了此接口的类称为可枚举类型,是可以用foreach进行遍历的标志。
方法GetEnumerator()的返回值是枚举器,可以理解为游标。
以下是自定义一个迭代器的示例(https://msdn.microsoft.com/en-us/library/system.collections.ienumerator.aspx):
using System;using System.Collections;// Simple business object.public class Person{ public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName;}// Collection of Person objects. This class// implements IEnumerable so that it can be used// with ForEach syntax.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]; } }// Implementation for the GetEnumerator method. IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator) GetEnumerator(); } public PeopleEnum GetEnumerator() { return new PeopleEnum(_people); }}// When you implement IEnumerable, you must also implement IEnumerator.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; } public bool MoveNext() { position++; return (position < _people.Length); } 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 App{ static void Main() { 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); }}/* This code produces output similar to the following: * * John Smith * Jim Johnson * Sue Rabon * */在有了yield这个关键字以后,我们可以通过这样的方式来创建枚举器:
using System;using System.Collections;// Simple business object.public class Person{ public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } public string firstName; public string lastName;}// Collection of Person objects. This class// implements IEnumerable so that it can be used// with ForEach syntax.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]; } } // Implementation for the GetEnumerator method. IEnumerator IEnumerable.GetEnumerator() { for (int i = 0; i < _people.Length; i++) { yield return _people[i]; } }}class App{ static void Main() { 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); }}以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了C#中foreach原理以及模拟的实现方法,分享给大家供大家参考。具体如下:复制代码代码如下:publicclassPerson:IEnumera
foreach遍历是C#常见的功能,而本文通过实例形式展现了C#使用yield关键字让自定义集合实现foreach遍历的方法。具体步骤如下:一般来说当我们创建自
本文实例讲述了C#中foreach语句使用break暂停遍历的方法。分享给大家供大家参考。具体分析如下:下面的代码演示了在C#中使用foreach时如何通过br
本文实例讲述了C#使用foreach遍历哈希表(hashtable)的方法。分享给大家供大家参考。具体实现方法如下:usingSystem;usingSyste
1.foreach语句C#编译器会把foreach语句转换为IEnumerable接口的方法和属性。复制代码代码如下:foreach(Personpinpers