C#中IEnumerable接口介紹并實現(xiàn)自定義集合
簡介
IEnumerable接口是非常的簡單,只包含一個抽象的方法GetEnumerator(),它返回一個可用于循環(huán)訪問集合的IEnumerator對象。對于所有數(shù)組的遍歷,都來自IEnumerable接口。
IEnumerator對象有什么呢?它是一個真正的集合訪問器,沒有它,就不能使用foreach語句遍歷集合或數(shù)組,因為只有IEnumerator對象才能訪問集合中的項,假如連集合中的項都訪問不了,那么進(jìn)行集合的循環(huán)遍歷是不可能的事情了。
一、foreach在IEnumerable中案例
public static void Test3()
{
MyInt temp = new MyInt();
foreach (int item in temp)
Console.WriteLine(item);
}
//foreach的必須要實現(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)前項,相當(dāng)于把指針移到初始位置:position = -1; 一開始認(rèn)識數(shù)組的索引從“0”開始
myie.Reset();
//向前移動一個索引,返回Bool類型,判斷是否超出下標(biāo)
while (myie.MoveNext())
{
int i = (int)myie.Current;//從Object轉(zhuǎn)成對應(yīng)類型
Console.WriteLine("Value: {0}", i);
}
}包含一個屬性兩個方法
MoveNext:把當(dāng)前的項移動到下一項(類似于索引值),返回一個bool值,這個bool值用來檢查當(dāng)前項是否超出了枚舉數(shù)的范圍!
Current:獲取當(dāng)前項的值,返回一個object的類型!
Reset:顧名思義也就是把一些值恢復(fù)為默認(rèn)值,比如把當(dā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中的元素長度小于6的值,然后打印出。
實現(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;
}
//實現(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)
{
//實例化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接口并實現(xiàn)自定義集合的文章就介紹到這了。希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
C#實現(xiàn)高性能Excel百萬數(shù)據(jù)導(dǎo)出優(yōu)化實戰(zhàn)指南
在日常工作中,Excel數(shù)據(jù)導(dǎo)出是一個常見的需求,然而,當(dāng)數(shù)據(jù)量較大時,性能和內(nèi)存問題往往會成為限制導(dǎo)出效率的瓶頸,下面我們看看C#如何結(jié)合EPPlus,MiniExcel和NPOI提高導(dǎo)出效率吧2025-05-05
C# 利用Aspose.Words.dll將 Word 轉(zhuǎn)成PDF
關(guān)于word轉(zhuǎn)成pdf的方法網(wǎng)上有很多。大部分需要借助office 2007及以上版本的組件。安裝配置起來比較麻煩。今天偶然得之“Aspose.Words.dll”可以實現(xiàn)2013-08-08
基于WPF平臺使用純C#實現(xiàn)動態(tài)處理json字符串
在當(dāng)今的軟件開發(fā)領(lǐng)域,數(shù)據(jù)的交換與存儲變得愈發(fā)頻繁,JSON作為一種輕量級的數(shù)據(jù)交換格式,在 WPF平臺開發(fā)的桌面應(yīng)用里,我們常常需要與各種數(shù)據(jù)源交互,動態(tài)處理JSON字符串就成為了一項必備技能,本文將深入探討如何在 WPF 平臺上,僅使用純C#代碼實現(xiàn)對JSON字符串的動態(tài)處理2025-01-01
C#實現(xiàn)統(tǒng)計100以內(nèi)所有素數(shù)的個數(shù)
這篇文章介紹了C#實現(xiàn)統(tǒng)計100以內(nèi)所有素數(shù)個數(shù)的方法,文中注釋非常詳細(xì),很適合新手學(xué)習(xí)。對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2021-11-11

