通過一個例子來看

創(chuàng)新互聯(lián)建站專注于企業(yè)成都全網(wǎng)營銷、網(wǎng)站重做改版、松原網(wǎng)站定制設(shè)計、自適應(yīng)品牌網(wǎng)站建設(shè)、HTML5、商城網(wǎng)站建設(shè)、集團公司官網(wǎng)建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、高端網(wǎng)站制作、響應(yīng)式網(wǎng)頁設(shè)計等建站業(yè)務(wù),價格優(yōu)惠性價比高,為松原等各大城市提供網(wǎng)站開發(fā)制作服務(wù)。
-------------------------------------------------------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對象
Student s;
//游標
int i = -1;
public StiAll(Student ss)//構(gòu)造函數(shù),初始化student對象
{
this.s = ss;
}
public object Current//獲取當前的項(只讀屬性)
{
get { return s.s[i]; }
}
public bool MoveNext()//將游標的位置向前移動
{
if (i<s.s.Length-1)//如果在s數(shù)組的長度范圍之內(nèi)就返回true
{
i++;
return true;
}
else
{
return false;
}
}
public void Reset()//初始化游標
{
i = -1;
}
}
}-------------------------------------------------------主程序
Student s = new Student(new string[] { "呂蒙", "周泰", "黃蓋" });//實例化Student對象
//第一種方式遍歷
foreach (var item in s)
{
Console.WriteLine(item);//輸出呂蒙,周泰,黃蓋
}
//第二種方式遍歷
StiAll sa = new StiAll(s);
while (sa.MoveNext())
{
Console.WriteLine(sa.Current);//輸出呂蒙,周泰,黃蓋
}
Console.ReadKey();
分享名稱:IEnumerable接口與IEnumerator接口
URL標題:http://chinadenli.net/article8/pgspip.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、外貿(mào)網(wǎng)站建設(shè)、網(wǎng)頁設(shè)計公司、建站公司、用戶體驗、App設(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)