C# IEnumerable和IEnumerator接口淺析
溫故而知新,可以為師矣,有空經(jīng)常復(fù)習(xí)一下基礎(chǔ)知識(shí)是有必要的,并且能加深理解和記憶。
Foreach常用于循環(huán)訪問集合,對(duì)實(shí)現(xiàn)IEnumerable的接口的容器進(jìn)行遍歷,IEnumerable和IEnumerator接口我有時(shí)候也有點(diǎn)迷糊,按官方的解釋,IEnumerable是枚舉器接口,IEnumerator是迭代器接口,從字面意思來看相差不大,逐一分析一下。
IEnumerable接口
public interface IEnumerable { IEnumerator GetEnumerator(); }
繼承IEnumerable接口的類需實(shí)現(xiàn)暴露出來的GetEnumerator()方法,并返回一個(gè)IEnumerator接口對(duì)象,看來真正做事的是IEnumerator,F(xiàn)12看一下IEnumerator又有什么鬼東西。
IEnumerator接口
public interface IEnumerator { object Current { get; } bool MoveNext(); void Reset(); }
IEnumerator接口有三個(gè)東東,一個(gè)屬性Current,返回當(dāng)前集合中的元素,方法MoveNext()移動(dòng)到下一個(gè),遍歷不都是向后遍歷的嘛,Reset(),字面意思重置,這個(gè)容易理解。做個(gè)假設(shè):既然IEnumerable接口返回是IEnumerator接口迭代器來實(shí)現(xiàn)的,那么僅繼承IEnumerator迭代器接口能不能實(shí)現(xiàn)一個(gè)自定義容器?
定義一個(gè)Phone類
public class Phone { public string Name; public Phone(string name) { this.Name = name; } }
定義一個(gè)名為MyEnumerator迭代器,并現(xiàn)實(shí)它接口IEnumerator
public class MyEnumerator : IEnumerator { Phone[] p; int idx = -1; public MyEnumerator(Phone[] t) { p = t; } public object Current { get { if (idx == -1) return new IndexOutOfRangeException(); return p[idx]; } } public bool MoveNext() { idx++; return p.Length > idx; } public void Reset() { idx = -1; } }
class Program { static void Main(string[] args) { show("-----------IEnumerator------------"); Phone[] phones = new Phone[] { new Phone("iPhone 7s"), new Phone("iPhone 6s"), new Phone("iPhone 5s") }; MyEnumerator enumerator = new MyEnumerator(phones); while (enumerator.MoveNext()) { Phone p = enumerator.Current as Phone; show(p.Name); } Console.ReadKey(); } static void show(string i) { Console.WriteLine(i); } }
結(jié)果顯示:
果然不出所料,真正做事情的是IEnumerator接口,即可循環(huán)訪問自定義的一個(gè)容器,不過,初衷是想用Foreach來做循環(huán)訪問、遍歷的。那好,那就只能顯示IEnumerable接口來做。稍稍改造一下Phone類:
public class Phone : IEnumerable { public string Name ; public Phone(string name) { this.Name = name; } Phone[] p; public Phone(Phone[] t) { p = t; } public IEnumerator GetEnumerator() { return new MyEnumerator(p); } }
static void Main(string[] args) { show("-----------IEnumerator------------"); Phone[] phones = new Phone[] { new Phone("iPhone 7s"), new Phone("iPhone 6s"), new Phone("iPhone 5s") }; MyEnumerator enumerator = new MyEnumerator(phones); while (enumerator.MoveNext()) { Phone p = enumerator.Current as Phone; show(p.Name); } show("-----------IEnumerable------------"); Phone phoneList = new Phone(phones); foreach (Phone p in phoneList) { show(p.Name); } Console.ReadKey(); }
結(jié)果顯示:
大功告成,再擴(kuò)展成通用的容器PhonePackage,繼承泛型IEnumerable<T>接口即可。
public class PhonePackage<T> : IEnumerable<T> { private List<T> dataList = null; public void Add(T t) { if (dataList == null) dataList = new List<T>(); dataList.Add(t); } public IEnumerator<T> GetEnumerator() { foreach (T t in dataList) { yield return t; } } IEnumerator IEnumerable.GetEnumerator() { foreach (T t in dataList) { yield return t; } } }
static void Main(string[] args) { show("-----------IEnumerator------------"); Phone[] phones = new Phone[] { new Phone("iPhone 7s"), new Phone("iPhone 6s"), new Phone("iPhone 5s") }; MyEnumerator enumerator = new MyEnumerator(phones); while (enumerator.MoveNext()) { Phone p = enumerator.Current as Phone; show(p.Name); } show("-----------IEnumerable------------"); Phone phoneList = new Phone(phones); foreach (Phone p in phoneList) { show(p.Name); } show("-----------IEnumerable<T>------------"); PhonePackage<Phone> phonePackage = new PhonePackage<Phone>(); phonePackage.Add(new Phone("iPhone 7s")); phonePackage.Add(new Phone("iPhone 6s")); phonePackage.Add(new Phone("iPhone 5s")); foreach (Phone p in phonePackage) { show(p.Name); } Console.ReadKey(); } static void show(string i) { Console.WriteLine(i); }
結(jié)果顯示:
IEnumerator迭代器接口挺啰嗦的,yield是簡化了遍歷的語法糖而已。
以上就是本文的全部內(nèi)容,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作能帶來一定的幫助,同時(shí)也希望多多支持腳本之家!
相關(guān)文章
python實(shí)現(xiàn)AutoResetEvent類的阻塞模式方法解析
AutoResetEvent :當(dāng)某個(gè)線程執(zhí)行到WaitOne()方法時(shí),該線程則會(huì)處于阻塞模式,當(dāng)被調(diào)用了Set()方法,阻塞的線程則會(huì)繼續(xù)向下執(zhí)行,其狀態(tài)立即被自動(dòng)設(shè)置為阻塞模式2012-11-11C#?使用SpecFlow創(chuàng)建BDD測試用例的示例代碼
這篇文章主要介紹了C#?使用SpecFlow創(chuàng)建BDD測試用例,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-06-06Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn)
這篇文章主要為大家介紹了Unity的AssetPostprocessor之Model函數(shù)使用實(shí)戰(zhàn),有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-08-08C#實(shí)現(xiàn)微信公眾號(hào)會(huì)員卡管理的示例代碼
這篇文章主要介紹了C#實(shí)現(xiàn)微信公眾號(hào)會(huì)員卡管理的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-06-06C#定義并實(shí)現(xiàn)單鏈表實(shí)例解析
這篇文章主要介紹了C#定義并實(shí)現(xiàn)單鏈表實(shí)例解析,有助于讀者加深對(duì)C#實(shí)現(xiàn)數(shù)據(jù)結(jié)構(gòu)的理解,需要的朋友可以參考下2014-07-07Winform基于多線程實(shí)現(xiàn)每隔1分鐘執(zhí)行一段代碼
這篇文章主要介紹了Winform基于多線程實(shí)現(xiàn)每隔1分鐘執(zhí)行一段代碼的方法,設(shè)計(jì)線程的操作及時(shí)間函數(shù)的用法,需要的朋友可以參考下2014-10-10