重慶分公司,新征程啟航
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
為企業(yè)提供網(wǎng)站建設(shè)、域名注冊(cè)、服務(wù)器等服務(wù)
通過一個(gè)例子來看
-------------------------------------------------------Student.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication6 { public class Student:IEnumerable { //數(shù)組 public string[] s; //索引器 public int i; public Student(string[] str)//構(gòu)造函數(shù),初始化數(shù)組 { s = str; } public IEnumerator GetEnumerator()//迭代器 { return s.GetEnumerator(); } } }
-------------------------------------------------------StiAll.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Collections; namespace ConsoleApplication6 { public class StiAll:IEnumerator { //student對(duì)象 Student s; //游標(biāo) int i = -1; public StiAll(Student ss)//構(gòu)造函數(shù),初始化student對(duì)象 { this.s = ss; } public object Current//獲取當(dāng)前的項(xiàng)(只讀屬性) { get { return s.s[i]; } } public bool MoveNext()//將游標(biāo)的位置向前移動(dòng) { if (i-------------------------------------------------------主程序
Student s = new Student(new string[] { "呂蒙", "周泰", "黃蓋" });//實(shí)例化Student對(duì)象 //第一種方式遍歷 foreach (var item in s) { Console.WriteLine(item);//輸出呂蒙,周泰,黃蓋 } //第二種方式遍歷 StiAll sa = new StiAll(s); while (sa.MoveNext()) { Console.WriteLine(sa.Current);//輸出呂蒙,周泰,黃蓋 } Console.ReadKey();創(chuàng)新互聯(lián)www.cdcxhl.cn,專業(yè)提供香港、美國云服務(wù)器,動(dòng)態(tài)BGP最優(yōu)骨干路由自動(dòng)選擇,持續(xù)穩(wěn)定高效的網(wǎng)絡(luò)助力業(yè)務(wù)部署。公司持有工信部辦法的idc、isp許可證, 機(jī)房獨(dú)有T級(jí)流量清洗系統(tǒng)配攻擊溯源,準(zhǔn)確進(jìn)行流量調(diào)度,確保服務(wù)器高可用性。佳節(jié)活動(dòng)現(xiàn)已開啟,新人活動(dòng)云服務(wù)器買多久送多久。
分享名稱:IEnumerable接口與IEnumerator接口-創(chuàng)新互聯(lián)
標(biāo)題鏈接:http://www.xueling.net.cn/article/jdhgh.html