C#中foreach語(yǔ)句深入研究
1、概述
本文通過(guò)手動(dòng)實(shí)現(xiàn)迭代器來(lái)了解foreach語(yǔ)句的本質(zhì)。
2、使用foreach語(yǔ)句遍歷集合
在C#中,使用foreach語(yǔ)句來(lái)遍歷集合。foreach語(yǔ)句是微軟提供的語(yǔ)法糖,使用它可以簡(jiǎn)化C#內(nèi)置迭代器的使用復(fù)雜性。編譯foreach語(yǔ)句,會(huì)生成調(diào)用GetEnumerator和MoveNext方法以及Current屬性的代碼,這些方法和屬性恰是C#內(nèi)置迭代器所提供的。下面將通過(guò)實(shí)例來(lái)說(shuō)明這一切。
例1:使用foreach來(lái)遍歷集合
//************************************************************ // // foreach應(yīng)用示例代碼 // // Author:三五月兒 // // Date:2014/09/10 // // //************************************************************ using System; using System.Collections; using System.Collections.Generic; namespace IEnumerableExp { class Program { static void Main(string[] args) { List<Student> studentList = new List<Student>() { new Student(){Id = 1, Name = "三五月兒", Age = 23}, new Student(){Id = 2, Name = "張三豐", Age = 108}, new Student(){Id = 3, Name = "艾爾克森", Age = 25}, new Student(){Id = 3, Name = "穆里奇", Age = 27} }; foreach (var student in studentList) { Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", student.Id,student.Name,student.Age); } } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } }
代碼中,使用foreach語(yǔ)句遍歷Student對(duì)象的集合,依次輸出Student對(duì)象的Id,Name,Age屬性值。使用ILDASM查看程序?qū)?yīng)的IL代碼,下面這些是與foreach語(yǔ)句相關(guān)的IL代碼:
IL_00c6: callvirt instance valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<!0> class [mscorlib]System.Collections.Generic.List`1<class IEnumerableExp.Student>::GetEnumerator() IL_00d1: call instance !0 valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class IEnumerableExp.Student>::get_Current() IL_0102: call instance bool valuetype [mscorlib]System.Collections.Generic.List`1/Enumerator<class IEnumerableExp.Student>::MoveNext()
在IL代碼中,是不是找到了GetEnumerator和MoveNext方法以及Current屬性的身影,可見(jiàn):foreach語(yǔ)句確實(shí)是微軟提供的用來(lái)支持C#內(nèi)置迭代器操作的語(yǔ)法糖,因?yàn)檫@些方法和屬性正是C#內(nèi)置迭代器所提供的。
當(dāng)然,除了使用foreach語(yǔ)句來(lái)遍歷集合外,還可以使用C#內(nèi)置迭代器提供的方法和屬性來(lái)遍歷集合,本例中還可以使用下面的代碼來(lái)完成遍歷操作:
IEnumerator<Student> studentEnumerator = studentList.GetEnumerator(); while (studentEnumerator.MoveNext()) { var currentStudent = studentEnumerator.Current as Student; Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", currentStudent.Id, currentStudent.Name, currentStudent.Age); }
在第二種方法中,通過(guò)調(diào)用GetEnumerator和MoveNext方法以及Current屬性來(lái)完成遍歷操作,是不是與foreach語(yǔ)句編譯后生成的代碼一致啊。
兩種遍歷方法,都會(huì)得到下圖所示結(jié)果:
圖1 遍歷集合元素
查看代碼中GetEnumerator和MoveNext方法以及Current屬性的定義,發(fā)現(xiàn)GetEnumerator方法來(lái)自于IEnumerable接口,而MoveNext方法與Current屬性來(lái)自于IEnumerator接口。實(shí)現(xiàn)C#迭代器都應(yīng)該實(shí)現(xiàn)這兩個(gè)接口。下面就手動(dòng)實(shí)現(xiàn)一個(gè)迭代器來(lái)操作學(xué)生對(duì)象的集合。
3、手動(dòng)實(shí)現(xiàn)一個(gè)迭代器
前面使用到的是C#內(nèi)置迭代器,當(dāng)然,我們完全可以手動(dòng)實(shí)現(xiàn)一個(gè)自己的迭代器。
例2:手動(dòng)實(shí)現(xiàn)迭代器
//************************************************************ // // foreach應(yīng)用示例代碼 // // Author:三五月兒 // // Date:2014/09/10 // // //************************************************************ using System; using System.Collections; using System.Collections.Generic; namespace IEnumerableExp { class Program { static void Main(string[] args) { Student[] students = new Student[4] { new Student(){Id = 1, Name = "三五月兒", Age = 23}, new Student(){Id = 2, Name = "張三豐", Age = 108}, new Student(){Id = 3, Name = "艾爾克森", Age = 25}, new Student(){Id = 3, Name = "穆里奇", Age = 27} }; StudentSet studentSet = new StudentSet(students); foreach (var student in studentSet) { Console.WriteLine("Id = {0}, Name = {1}, Age = {2}", student.Id, student.Name, student.Age); } } } public class Student { public int Id { get; set; } public string Name { get; set; } public int Age { get; set; } } public class StudentSet : IEnumerable { private Student[] students; public StudentSet(Student[] inputStudents) { students = new Student[inputStudents.Length]; for (int i = 0; i < inputStudents.Length; i++) { students[i] = inputStudents[i]; } } IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)GetEnumerator(); } public StudentEnumerator GetEnumerator() { return new StudentEnumerator(students); } } public class StudentEnumerator : IEnumerator { public Student[] students; int position = -1; public StudentEnumerator(Student[] students) { this.students = students; } public bool MoveNext() { position++; return (position < students.Length); } public void Reset() { position = -1; } object IEnumerator.Current { get { return Current; } } public Student Current { get { try { return students[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } }
代碼中定義學(xué)生集合類(lèi)StudentSet,在類(lèi)中使用Student類(lèi)型的數(shù)組來(lái)保存學(xué)生元素,該類(lèi)實(shí)現(xiàn)IEnumerable接口,所以StudentSet類(lèi)必須實(shí)現(xiàn)IEnumerable接口的GetEnumerator方法,該方法返回實(shí)現(xiàn)了IEnumerator接口的迭代器StudentEnumerator。
下面來(lái)看看StudentEnumerator類(lèi)的定義,StudentEnumerator表示遍歷學(xué)生集合的迭代器,使用它提供的方法和屬性可以遍歷集合的元素,該類(lèi)實(shí)現(xiàn)IEnumerator接口,所以必須實(shí)現(xiàn)IEnumerator接口提供的MoveNext和Reset方法以及Current屬性。StudentEnumerator類(lèi)使用Student類(lèi)型的集合students來(lái)保存需要遍歷的集合。使用私有變量position來(lái)記錄元素的位置,一開(kāi)始position被賦值為-1,定位于集合中第一個(gè)元素的前面,在Reset方法中也可以將position的值置為-1,表示回到遍歷操作前的狀態(tài)。在MoveNext方法中先將position加1,再將其與集合的長(zhǎng)度進(jìn)行比較,看是否已經(jīng)遍歷完了所有元素,若未完返回true,否則返回false。在只讀屬性Current的實(shí)現(xiàn)中通過(guò)代碼students[position]返回students集合中position位置的元素值。在使用迭代器時(shí),需要先調(diào)用MoveNext方法判斷下一個(gè)元素是否存在,如存在使用Current屬性得到這個(gè)值,若不存在則表示已經(jīng)遍歷完所有元素,將停止遍歷操作。
代碼中同樣使用foreach語(yǔ)句來(lái)遍歷StudentSet對(duì)象中的元素并輸出,與使用內(nèi)置迭代器的效果一致。
4、總結(jié)
實(shí)現(xiàn)迭代器需要借助于IEnumerable與IEnumerator接口,接口IEnumerator提供的方法GetEnumerator可以返回實(shí)現(xiàn)IEnumerator接口的迭代器,而IEnumerator接口中包含了實(shí)現(xiàn)迭代器所需的方法及屬性的定義。凡是實(shí)現(xiàn)了迭代器的類(lèi)都可以使用foreach語(yǔ)句來(lái)遍歷其元素,因?yàn)閒oreach語(yǔ)句是微軟提供的支持內(nèi)置迭代器的語(yǔ)法糖,編譯foreach語(yǔ)句后生成的代碼與使用迭代器的代碼完全一致。
- C#中foreach語(yǔ)句使用break暫停遍歷的方法
- C#使用foreach語(yǔ)句遍歷隊(duì)列(Queue)的方法
- C#使用foreach語(yǔ)句簡(jiǎn)單遍歷數(shù)組的方法
- C#使用foreach語(yǔ)句遍歷二維數(shù)組的方法
- 深入理解C#中foreach遍歷的使用方法
- C#使用foreach循環(huán)遍歷數(shù)組完整實(shí)例
- 淺談C#中的for循環(huán)與foreach循環(huán)
- C# 遍歷枚舉類(lèi)型的所有元素
- C# 獲取枚舉值的簡(jiǎn)單實(shí)例
- C#中Foreach循環(huán)遍歷的本質(zhì)與枚舉器詳解
相關(guān)文章
C#編程實(shí)現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài)
這篇文章主要介紹了C#編程實(shí)現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài),本文直接給出實(shí)例代碼,需要的朋友可以參考下2015-06-06基于WebRequest.RegisterPrefix的使用詳解
本篇文章對(duì)WebRequest.RegisterPrefix的使用進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05C# 獲取數(shù)據(jù)庫(kù)中所有表名、列名的示例代碼
這篇文章主要介紹了C# 獲取數(shù)據(jù)庫(kù)中所有表名、列名,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06利用lambda表達(dá)式樹(shù)優(yōu)化反射詳解
這篇文章主要給大家介紹了關(guān)于如何利用lambda表達(dá)式樹(shù)優(yōu)化反射的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12Unity3D啟動(dòng)外部程序并傳遞參數(shù)的實(shí)現(xiàn)
這篇文章主要介紹了Unity3D啟動(dòng)外部程序并傳遞參數(shù)的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-04-04c#計(jì)算某段代碼的執(zhí)行時(shí)間實(shí)例方法
在本篇文章里我們給大家整理了關(guān)于c#計(jì)算某段代碼的執(zhí)行時(shí)間的方法和經(jīng)驗(yàn),有興趣的朋友們學(xué)習(xí)下。2019-02-02C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03