C#使用yield關(guān)鍵字構(gòu)建迭代器詳解
以前,如果我們希望構(gòu)建支持foreach枚舉的自定義集合,只能實(shí)現(xiàn)IEnumerable接口(可能還有IEnumerator()),返回值還必須是IEnumerator類型,除此之外還可以通過迭代器來使用構(gòu)建foreach循環(huán)的類型,詳細(xì)見下鏈接。
代碼
public class Car { //內(nèi)部狀態(tài)數(shù)據(jù) public int CurentSpeed; public int MaxSpeed; public string name; //汽車能不能用 private bool carIsdead; //類構(gòu)造函數(shù) public Car() { } public Car(string name, int currentspeed, int maxspeed = 100) { this.name = name; this.CurentSpeed = currentspeed; this.MaxSpeed = maxspeed; } //定義委托類型 public delegate void CarEngineHandler(string msdForCar); //定義每個(gè)委托類型的成員變量 private CarEngineHandler listOfhandlers; //向調(diào)用者添加注冊(cè)函數(shù) public void RegisterWithCarEngine(CarEngineHandler methodTocall) { if (listOfhandlers == null) listOfhandlers = methodTocall; else listOfhandlers += methodTocall;//支持多路廣播 } //實(shí)現(xiàn)Accelerate()方法 public void Accelerate(int delta) { if (carIsdead) { if (listOfhandlers != null) { listOfhandlers("sorry,this car is dead"); } } else { CurentSpeed += delta; //不能超過最大速度 if (5 == (MaxSpeed - CurentSpeed) && listOfhandlers != null) { listOfhandlers("this speed is nearly to the maxspeed"); } if (CurentSpeed > MaxSpeed) { carIsdead = true; } else Console.WriteLine("current speed:{0}", CurentSpeed); } } } public class Garage : IEnumerable { private Car[] garage = new Car[3]; public Garage() { garage[0] = new Car("a", 10); garage[1] = new Car("b", 13); garage[2] = new Car("c", 14); } public Enumerator GetEnumerator() { //返回?cái)?shù)組對(duì)象的IEnumerator //return garage.GetEnumerator(); //用yield關(guān)鍵字構(gòu)建迭代器方法 foreach (Car c in garage) { //當(dāng)yield return語句執(zhí)行后,當(dāng)前位會(huì)被 //保存下來,下一次執(zhí)行會(huì)從當(dāng)前位開始 yield return c; } } } class Program { static void Main(string[] args) { Garage g = new Garage(); foreach (Car c in g) { Console.WriteLine("car name:{0}", c.name); } } }
參考:C#中可枚舉類型詳解
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。
上一篇:C#通過KD樹進(jìn)行距離最近點(diǎn)的查找
欄 目:C#教程
下一篇:C#中值類型和引用類型解析
本文標(biāo)題:C#使用yield關(guān)鍵字構(gòu)建迭代器詳解
本文地址:http://mengdiqiu.com.cn/a1/C_jiaocheng/5454.html
您可能感興趣的文章
- 01-10C#使用Dispose模式實(shí)現(xiàn)手動(dòng)對(duì)資源的釋放
- 01-10C#3.0使用EventLog類寫Windows事件日志的方法
- 01-10C#使用windows服務(wù)開啟應(yīng)用程序的方法
- 01-10c# ArrayList的使用方法小總結(jié)
- 01-10C#使用ADO.Net部件來訪問Access數(shù)據(jù)庫的方法
- 01-10C#使用Mutex簡(jiǎn)單實(shí)現(xiàn)程序單實(shí)例運(yùn)行的方法
- 01-10使用Nopcommerce為商城添加滿XX減XX優(yōu)惠券功能
- 01-10C#中yield用法使用說明
- 01-10C#編程和Visual Studio使用技巧(下)
- 01-10C#編程和Visual Studio使用技巧(上)


閱讀排行
- 1C語言 while語句的用法詳解
- 2java 實(shí)現(xiàn)簡(jiǎn)單圣誕樹的示例代碼(圣誕
- 3利用C語言實(shí)現(xiàn)“百馬百擔(dān)”問題方法
- 4C語言中計(jì)算正弦的相關(guān)函數(shù)總結(jié)
- 5c語言計(jì)算三角形面積代碼
- 6什么是 WSH(腳本宿主)的詳細(xì)解釋
- 7C++ 中隨機(jī)函數(shù)random函數(shù)的使用方法
- 8正則表達(dá)式匹配各種特殊字符
- 9C語言十進(jìn)制轉(zhuǎn)二進(jìn)制代碼實(shí)例
- 10C語言查找數(shù)組里數(shù)字重復(fù)次數(shù)的方法
本欄相關(guān)
- 01-10C#通過反射獲取當(dāng)前工程中所有窗體并
- 01-10關(guān)于ASP網(wǎng)頁無法打開的解決方案
- 01-10WinForm限制窗體不能移到屏幕外的方法
- 01-10WinForm繪制圓角的方法
- 01-10C#實(shí)現(xiàn)txt定位指定行完整實(shí)例
- 01-10WinForm實(shí)現(xiàn)仿視頻 器左下角滾動(dòng)新
- 01-10C#停止線程的方法
- 01-10C#實(shí)現(xiàn)清空回收站的方法
- 01-10C#通過重寫Panel改變邊框顏色與寬度的
- 01-10C#實(shí)現(xiàn)讀取注冊(cè)表監(jiān)控當(dāng)前操作系統(tǒng)已
隨機(jī)閱讀
- 01-11ajax實(shí)現(xiàn)頁面的局部加載
- 04-02jquery與jsp,用jquery
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打開原生自帶讀寫NTFS功能(圖文
- 08-05織夢(mèng)dedecms什么時(shí)候用欄目交叉功能?
- 01-10使用C語言求解撲克牌的順子及n個(gè)骰子
- 01-10C#中split用法實(shí)例總結(jié)
- 08-05DEDE織夢(mèng)data目錄下的sessions文件夾有什
- 08-05dedecms(織夢(mèng))副欄目數(shù)量限制代碼修改
- 01-10SublimeText編譯C開發(fā)環(huán)境設(shè)置