C#中foreach語句深入研究
1、概述
本文通過手動實現(xiàn)迭代器來了解foreach語句的本質(zhì)。
2、使用foreach語句遍歷集合
在C#中,使用foreach語句來遍歷集合。foreach語句是微軟提供的語法糖,使用它可以簡化C#內(nèi)置迭代器的使用復(fù)雜性。編譯foreach語句,會生成調(diào)用GetEnumerator和MoveNext方法以及Current屬性的代碼,這些方法和屬性恰是C#內(nèi)置迭代器所提供的。下面將通過實例來說明這一切。
例1:使用foreach來遍歷集合
//************************************************************ // // 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語句遍歷Student對象的集合,依次輸出Student對象的Id,Name,Age屬性值。使用ILDASM查看程序?qū)?yīng)的IL代碼,下面這些是與foreach語句相關(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屬性的身影,可見:foreach語句確實是微軟提供的用來支持C#內(nèi)置迭代器操作的語法糖,因為這些方法和屬性正是C#內(nèi)置迭代器所提供的。
當(dāng)然,除了使用foreach語句來遍歷集合外,還可以使用C#內(nè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); }
在第二種方法中,通過調(diào)用GetEnumerator和MoveNext方法以及Current屬性來完成遍歷操作,是不是與foreach語句編譯后生成的代碼一致啊。
兩種遍歷方法,都會得到下圖所示結(jié)果:
圖1 遍歷集合元素
查看代碼中GetEnumerator和MoveNext方法以及Current屬性的定義,發(fā)現(xiàn)GetEnumerator方法來自于IEnumerable接口,而MoveNext方法與Current屬性來自于IEnumerator接口。實現(xiàn)C#迭代器都應(yīng)該實現(xiàn)這兩個接口。下面就手動實現(xiàn)一個迭代器來操作學(xué)生對象的集合。
3、手動實現(xiàn)一個迭代器
前面使用到的是C#內(nèi)置迭代器,當(dāng)然,我們完全可以手動實現(xiàn)一個自己的迭代器。
例2:手動實現(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é)生集合類StudentSet,在類中使用Student類型的數(shù)組來保存學(xué)生元素,該類實現(xiàn)IEnumerable接口,所以StudentSet類必須實現(xiàn)IEnumerable接口的GetEnumerator方法,該方法返回實現(xiàn)了IEnumerator接口的迭代器StudentEnumerator。
下面來看看StudentEnumerator類的定義,StudentEnumerator表示遍歷學(xué)生集合的迭代器,使用它提供的方法和屬性可以遍歷集合的元素,該類實現(xiàn)IEnumerator接口,所以必須實現(xiàn)IEnumerator接口提供的MoveNext和Reset方法以及Current屬性。StudentEnumerator類使用Student類型的集合students來保存需要遍歷的集合。使用私有變量position來記錄元素的位置,一開始position被賦值為-1,定位于集合中第一個元素的前面,在Reset方法中也可以將position的值置為-1,表示回到遍歷操作前的狀態(tài)。在MoveNext方法中先將position加1,再將其與集合的長度進行比較,看是否已經(jīng)遍歷完了所有元素,若未完返回true,否則返回false。在只讀屬性Current的實現(xiàn)中通過代碼students[position]返回students集合中position位置的元素值。在使用迭代器時,需要先調(diào)用MoveNext方法判斷下一個元素是否存在,如存在使用Current屬性得到這個值,若不存在則表示已經(jīng)遍歷完所有元素,將停止遍歷操作。
代碼中同樣使用foreach語句來遍歷StudentSet對象中的元素并輸出,與使用內(nèi)置迭代器的效果一致。
4、總結(jié)
實現(xiàn)迭代器需要借助于IEnumerable與IEnumerator接口,接口IEnumerator提供的方法GetEnumerator可以返回實現(xiàn)IEnumerator接口的迭代器,而IEnumerator接口中包含了實現(xiàn)迭代器所需的方法及屬性的定義。凡是實現(xiàn)了迭代器的類都可以使用foreach語句來遍歷其元素,因為foreach語句是微軟提供的支持內(nèi)置迭代器的語法糖,編譯foreach語句后生成的代碼與使用迭代器的代碼完全一致。
相關(guān)文章
C#編程實現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài)
這篇文章主要介紹了C#編程實現(xiàn)向并口設(shè)備發(fā)送指令、獲取并口設(shè)備的狀態(tài),本文直接給出實例代碼,需要的朋友可以參考下2015-06-06基于WebRequest.RegisterPrefix的使用詳解
本篇文章對WebRequest.RegisterPrefix的使用進行了詳細的分析介紹,需要的朋友參考下2013-05-05C# 獲取數(shù)據(jù)庫中所有表名、列名的示例代碼
這篇文章主要介紹了C# 獲取數(shù)據(jù)庫中所有表名、列名,本文通過實例代碼給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06Unity3D啟動外部程序并傳遞參數(shù)的實現(xiàn)
這篇文章主要介紹了Unity3D啟動外部程序并傳遞參數(shù)的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04C#中使用HttpPost調(diào)用WebService的方法
這篇文章介紹了C#中使用HttpPost調(diào)用WebService的方法,文中通過示例代碼介紹的非常詳細。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-03-03