=================================================簡單的實現(xiàn)IEnumerable接口
------------------------------------Person.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication6 { public class Person { public string Name { get; set; } } }
------------------------------------PerAll.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication6 { public class PerAll:IEnumerable { Person[] p = new Person[3];//實例化長度為三個的數(shù)組 public PerAll()//構(gòu)造函數(shù)初始化數(shù)組 { p[0] = new Person { Name = "張三" }; p[1] = new Person { Name = "李四" }; p[2] = new Person { Name = "王五" }; } public IEnumerator GetEnumerator()//迭代器 { return p.GetEnumerator();//直接調(diào)用數(shù)組自帶的的GetEnumerator。(簡單委托請求到System.Array) } } }
------------------------------------主程序
PerAll p = new PerAll();//實例化對象 //-------------------第一種方式遍歷 foreach (Person item in p) { Console.WriteLine(item.Name); } //-------------------第二中方式遍歷 IEnumerator ie = p.GetEnumerator(); while (ie.MoveNext()) { Console.WriteLine((ie.Current as Person).Name); } //普通數(shù)組遍歷 string[] str = new string[3] { "張遼", "張合", "張飛" }; IEnumerator strie = str.GetEnumerator(); while (strie.MoveNext()) { Console.WriteLine(strie.Current as string); }
創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動態(tài)BGP最優(yōu)骨干路由自動選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機房獨有T級流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動現(xiàn)已開啟,新人活動云服務(wù)器買多久送多久。
文章名稱:IEnumerable接口-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://chinadenli.net/article26/ddihjg.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)、建站公司、響應(yīng)式網(wǎng)站、網(wǎng)站營銷、微信公眾號、網(wǎng)頁設(shè)計公司
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容