C#中IEnumerable接口介紹并實(shí)現(xiàn)自定義集合
簡(jiǎn)介
IEnumerable接口是非常的簡(jiǎn)單,只包含一個(gè)抽象的方法GetEnumerator(),它返回一個(gè)可用于循環(huán)訪問(wèn)集合的IEnumerator對(duì)象。對(duì)于所有數(shù)組的遍歷,都來(lái)自IEnumerable接口。
IEnumerator對(duì)象有什么呢?它是一個(gè)真正的集合訪問(wèn)器,沒(méi)有它,就不能使用foreach語(yǔ)句遍歷集合或數(shù)組,因?yàn)橹挥蠭Enumerator對(duì)象才能訪問(wèn)集合中的項(xiàng),假如連集合中的項(xiàng)都訪問(wèn)不了,那么進(jìn)行集合的循環(huán)遍歷是不可能的事情了。
一、foreach在IEnumerable中案例
public static void Test3() { MyInt temp = new MyInt(); foreach (int item in temp) Console.WriteLine(item); } //foreach的必須要實(shí)現(xiàn)IEnumerable和IEnumerator的接口 public class MyInt : IEnumerable { int[] temp = { 1, 32, 43, 343 }; public IEnumerator GetEnumerator() { return temp.GetEnumerator(); } }
相當(dāng)于下面代碼:
public static void Test1() { int[] myArray = { 1, 32, 43, 343 }; //獲取要遍歷的枚舉數(shù) IEnumerator myie = myArray.GetEnumerator(); //重置當(dāng)前項(xiàng),相當(dāng)于把指針移到初始位置:position = -1; 一開(kāi)始認(rèn)識(shí)數(shù)組的索引從“0”開(kāi)始 myie.Reset(); //向前移動(dòng)一個(gè)索引,返回Bool類型,判斷是否超出下標(biāo) while (myie.MoveNext()) { int i = (int)myie.Current;//從Object轉(zhuǎn)成對(duì)應(yīng)類型 Console.WriteLine("Value: {0}", i); } }
包含一個(gè)屬性兩個(gè)方法
MoveNext:把當(dāng)前的項(xiàng)移動(dòng)到下一項(xiàng)(類似于索引值),返回一個(gè)bool值,這個(gè)bool值用來(lái)檢查當(dāng)前項(xiàng)是否超出了枚舉數(shù)的范圍!
Current:獲取當(dāng)前項(xiàng)的值,返回一個(gè)object的類型!
Reset:顧名思義也就是把一些值恢復(fù)為默認(rèn)值,比如把當(dāng)前項(xiàng)恢復(fù)到默認(rèn)狀態(tài)值!
二、Lamda在IEnumerable中案例
//lamda表達(dá)式在數(shù)組中查詢 public static void Test2() { List<string> fruits = new List<string> { "apple", "passionfruit", "banana", "mango", "orange", "blueberry", "grape", "strawberry" }; //List<string> query = fruits.Where(fruit => fruit.Length < 6).ToList(); IEnumerable<string> query = fruits.Where(fruit => fruit.Length < 6); foreach (string fruit in query) Console.WriteLine(fruit); }
只篩選出List中的元素長(zhǎng)度小于6的值,然后打印出。
實(shí)現(xiàn)自定義集合的 IEnumerable和IEnumerator 接口
namespace ConsoleApplication1 { //定義Person類 public class Person { //初始化 public Person(string fName, string lName) { this.firstName = fName; this.lastName = lName; } //類成員 public string firstName; public string lastName; } //實(shí)現(xiàn)接口 public class People : IEnumerable { private Person[] _people; public People(Person[] pArray) { _people = new Person[pArray.Length]; for (int i = 0; i < pArray.Length; i++) { _people[i] = pArray[i]; } } IEnumerator IEnumerable.GetEnumerator() { return (IEnumerator)GetEnumerator(); } //獲取枚舉數(shù) public PeopleEnum GetEnumerator() { return new PeopleEnum(_people); } } public class PeopleEnum : IEnumerator { public Person[] _people; // Enumerators are positioned before the first element // until the first MoveNext() call. int position = -1; public PeopleEnum(Person[] list) { _people = list; } //向下推移索引,返回Bool類型值 public bool MoveNext() { position++; return (position < _people.Length); } //重置默認(rèn)索引位置,默認(rèn)下標(biāo)為0 public void Reset() { position = -1; } object IEnumerator.Current { get { return Current; } } //當(dāng)前索引值 public Person Current { get { try { return _people[position]; } catch (IndexOutOfRangeException) { throw new InvalidOperationException(); } } } } class Program { static void Main(string[] args) { //實(shí)例化Person Person[] peopleArray = new Person[3] { new Person("John", "Smith"), new Person("Jim", "Johnson"), new Person("Sue", "Rabon"), }; People peopleList = new People(peopleArray); foreach (Person p in peopleList) Console.WriteLine(p.firstName + " " + p.lastName); } } }
到此這篇關(guān)于C#中IEnumerable接口并實(shí)現(xiàn)自定義集合的文章就介紹到這了。希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- C#中IEnumerable、ICollection、IList、List之間的區(qū)別
- C# IQueryable及IEnumerable區(qū)別解析
- C# IEnumerable和IEnumerator接口淺析
- C#中IEnumerable、ICollection、IList、List之間的區(qū)別
- C#中IEnumerable接口用法實(shí)例分析
- C#中的IEnumerable簡(jiǎn)介及簡(jiǎn)單實(shí)現(xiàn)實(shí)例
- C#中的IEnumerable接口深入研究
- 基于C#中IDisposable與IEnumerable、IEnumerator的應(yīng)用
相關(guān)文章
C#打包應(yīng)用程序,與.NETFramework介紹
C#打包應(yīng)用程序,與.NETFramework介紹,需要的朋友可以參考一下2013-05-05C#中調(diào)用VB中Inputbox類的實(shí)現(xiàn)方法
本文主要介紹在項(xiàng)目中引用Microsoft.VisualBasic,間接使用VB中的各種類庫(kù)的方法,或者自己創(chuàng)建函數(shù),調(diào)用自定義方法,以實(shí)現(xiàn)InputBox相關(guān)的功能。2016-05-05C#實(shí)現(xiàn)學(xué)生管理系統(tǒng)
這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)學(xué)生管理系統(tǒng),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-08-08C#調(diào)用存儲(chǔ)過(guò)程詳解(帶返回值、參數(shù)輸入輸出等)
這篇文章主要介紹了C#調(diào)用存儲(chǔ)過(guò)程的方法,結(jié)合實(shí)例形式詳細(xì)分析了各種常用的存儲(chǔ)過(guò)程調(diào)用方法,包括帶返回值、參數(shù)輸入輸出等,需要的朋友可以參考下2016-06-06在Framework 4.0中:找出新增的方法與新增的類(一)
經(jīng)??吹接型瑢W(xué)在討論Framework 4 的新特性,新方法,于是想寫個(gè)程序找出framework4.0中新增的方法和類2013-05-05