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é)生集合類StudentSet,在類中使用Student類型的數(shù)組來(lái)保存學(xué)生元素,該類實(shí)現(xiàn)IEnumerable接口,所以StudentSet類必須實(shí)現(xiàn)IEnumerable接口的GetEnumerator方法,該方法返回實(shí)現(xiàn)了IEnumerator接口的迭代器StudentEnumerator。
下面來(lái)看看StudentEnumerator類的定義,StudentEnumerator表示遍歷學(xué)生集合的迭代器,使用它提供的方法和屬性可以遍歷集合的元素,該類實(shí)現(xiàn)IEnumerator接口,所以必須實(shí)現(xiàn)IEnumerator接口提供的MoveNext和Reset方法以及Current屬性。StudentEnumerator類使用Student類型的集合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)了迭代器的類都可以使用foreach語(yǔ)句來(lái)遍歷其元素,因?yàn)閒oreach語(yǔ)句是微軟提供的支持內(nèi)置迭代器的語(yǔ)法糖,編譯foreach語(yǔ)句后生成的代碼與使用迭代器的代碼完全一致。
相關(guān)文章
如何在Mac系統(tǒng)使用Visual Studio Code運(yùn)行Python
這篇文章主要介紹了Mac使用Visual Studio Code運(yùn)行Python環(huán)境的方法,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
C#面向?qū)ο髮?shí)現(xiàn)圖書管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#面向?qū)ο髮?shí)現(xiàn)圖書管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
Unity3D Shader實(shí)現(xiàn)貼圖切換效果
這篇文章主要為大家詳細(xì)介紹了Unity3D Shader實(shí)現(xiàn)貼圖切換效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-03-03
C#限速下載網(wǎng)絡(luò)文件的方法實(shí)例
本篇文章主要介紹了C#限速下載網(wǎng)絡(luò)文件的方法實(shí)例,可以限制下載文件的速度,非常具有實(shí)用價(jià)值,需要的朋友可以參考下。2016-12-12
C#利用OLEDB實(shí)現(xiàn)將DataTable寫入Excel文件中
這篇文章主要為大家詳細(xì)介紹了C#如何利用OLEDB實(shí)現(xiàn)將DataTable寫入Excel文件中,文中的示例代碼簡(jiǎn)潔易懂,具有一定的借鑒價(jià)值,需要的可以參考一下2023-02-02
C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法
這篇文章主要介紹了C#實(shí)現(xiàn)Excel導(dǎo)入sqlite的方法,是C#程序設(shè)計(jì)中非常重要的一個(gè)實(shí)用技巧,需要的朋友可以參考下2014-09-09

