IEnumerable的用法

开通VIP,畅享免费电子书等14项超值服

首页

好书

留言交流

下载APP

联系客服

2017.10.06

IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。

IEnumerator对象有什么呢?它是一个真正的集合访问器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。

一、IEnumerable、IEnumerator、ICollection、IList、List

IEnumerator:提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。IEnumerable:暴露一个IEnumerator,支持在普通集合中的遍历。

IEnumerator:继承自IEnumerator,有Current属性,返回的是T类型。IEnumerable:继承自IEnumerable,暴露一个IEnumerator,支持在泛型集合中遍历。

//摘要://公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。////类型参数://T://要枚举的对象的类型。[TypeDependency("System.SZArrayHelper")]publicinterfaceIEnumerable:IEnumerable{//摘要://返回一个循环访问集合的枚举器。////返回结果://可用于循环访问集合的System.Collections.Generic.IEnumerator。IEnumeratorGetEnumerator();}可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。

2、IEnumerator接口

publicinterfaceIEnumerator{boolMoveNext();//将游标的内部位置向前移动objectCurrent{get;}//获取当前的项(只读属性)voidReset();//将游标重置到第一个成员前面}3、ICollection接口:ICollection同时继承IEnumerable和IEnumerable两个接口

//摘要://定义操作泛型集合的方法。////类型参数://T://集合中元素的类型。[TypeDependency("System.SZArrayHelper")]publicinterfaceICollection:IEnumerable,IEnumerable4、IList接口

5、List的定义

publicclassList:IList,ICollection,IEnumerable,IList,ICollection,IEnumerable

THE END
1.C#IEnumerable.Where方法代码示例本文整理汇总了C#中IEnumerable.Where方法的典型用法代码示例。如果您正苦于以下问题:C# IEnumerable.Where方法的具体用法?C# IEnumerable.Where怎么用?C# IEnumerable.Where使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IEnumerable的用法示例。 https://vimsky.com/examples/detail/csharp-ex---IEnumerable-Where-method.html
2.(转)IEnumerable和IEnumerator详解namespaceMyCarIEnumerator{publicclassGarage:IEnumerable{Car[]carArray=newCar[4];//启动时填充一些Car对象publicGarage(){carArray[0]=newCar("Rusty",30);carArray[1]=newCar("Clunker",50);carArray[2]=newCar("Zippy",30);carArray[3]=newCar("Fred",45);}publicIEnumeratorGetEnumerator(){return...https://www.jianshu.com/p/7943bbd826d7
3.如何批量遍历IEnumerable然后可以在方法中调用它。public IEnumerable<user> GetBatch(int pageNumber){    return ...https://m.imooc.com/wenda/detail/603656
1.IEnumerable介面(System.Collections)MicrosoftLearnCast<TResult>(IEnumerable) 將IEnumerable 的項目轉換成指定的型別。 OfType<TResult>(IEnumerable) 根據指定的型別篩選 IEnumerable 的專案。 AsParallel(IEnumerable) 啟用查詢的平行處理。 AsQueryable(IEnumerable) 將IEnumerable 轉換成 IQueryable。 適用於 產品版本 .NET Core 1.0, Core 1.1, Core 2.0, Core...https://msdn.microsoft.com/zh-tw/library/h1x9x1b1(v=vs.90)
2.C#IEnumerator,IEnumerable,Iteratorienumerable<int>C# IEnumerator,IEnumerable ,Iterator IEnumerator 枚举器接口 在C#语言中,大部分以“I”字母开头命名的都是接口,所以情理之中,IEnumerator也是一个接口。 对于面向对象语言来说,接口就是一份“协议”,它定义了一组方法、属性和事件的契约,任何类、结构体或枚举只要符合这个契约,就可以被认为实现了该接口,可以被贴...https://blog.csdn.net/jshxjd/article/details/143665403
3.C#中IEnumerableICollectionIListList之间的区别C#教程按照功能排序:List<T> 《IList<T> 《ICollection<T>《IEnumerable<T> 按照性能排序:IEnumerable<T>《ICollection<T>《IList<T>《List<T> 另一种解释: ICollection 接口是 System.Collections 命名空间中类的基接口,ICollection 接口扩展 IEnumerable,IDictionary 和 IList 则是扩展 ICollection 的更为专用的接口。如果...https://www.jb51.net/article/218178.htm
4.C#中IEumerable的简单了解51CTO博客一、IEnumerable简单介绍 IEnumerable是可枚举类型,一般在迭代时应用广泛,如foreach中要循环访问的集合或数组都实现了IEnumerable接口。只要能够遍历,都直接或间接实现了IEnumerable接口。如:String类型的对象,可遍历,输出时以字符输出,间接实现了IEnumerable接口,"OOP"遍历打印就是'O','O','P';又如int类型没有实现IEnum...https://blog.51cto.com/u_4018548/6420389
5.StephenWaltheronASP.NETMVC6:IEnumerable GetCustomers(); 7: 8:} The IData interface has one method named GetCustomers() that, when implemented, should return a list of customers. Here’s a quick sample of how you can mock this interface with Typemock Isolator: ...https://weblogs.asp.net/stephenwalther/archive/2008/03/16/tdd-introduction-to-typemock-isolator.aspx
6....acommaseparatedlistfromIList<string>orIEnumerable<...What is the cleanest way to create a comma-separated list of string values from anIList<string>orIEnumerable<string>? String.Join(...)operates on astring[]so can be cumbersome to work with when types such asIList<string>orIEnumerable<string>cannot easily be converted into a string ...https://stackoverflow.com/questions/799446/creating-a-comma-separated-list-from-iliststring-or-ienumerablestring
7.DDD理论学习系列(12)仓储腾讯云开发者社区对于这种问题,我们最好在仓储中的方法中,比如List()或者ListActive()做默认处理,而不是在应用服务层每次去指定查询条件。 但具体是返回 IQueryable还是IEnumerable每个人的看法不一,具体可参考Repository 返回 IQueryable?还是 IEnumerable?。 5. 事务管理和工作单元...https://cloud.tencent.com/developer/article/1018485