=================================================簡單的實現(xiàn)IEnumerable接口
目前創(chuàng)新互聯(lián)建站已為成百上千家的企業(yè)提供了網(wǎng)站建設、域名、虛擬主機、綿陽服務器托管、企業(yè)網(wǎng)站設計、樂亭網(wǎng)站維護等服務,公司將堅持客戶導向、應用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。
------------------------------------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()//構造函數(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); }
網(wǎng)站名稱:IEnumerable接口
URL地址:http://bm7419.com/article18/jjejdp.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站排名、網(wǎng)頁設計公司、自適應網(wǎng)站、建站公司、關鍵詞優(yōu)化、域名注冊
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)