IEnumerator/IEnumerable接口
IEnumberator函数成员
- Current返回序列中当前位置项的 属性
- 只读属性
- 返回object类型
- MoveNext把枚举器位置前进到集合中下一项的方法
- 新位置有效返回true,否则false
- 枚举器原始位置在序列第一项前面,所以如果要用Current必须先用MoveNext()
- Reset枚举器位置重置为初始位置
IEnumerable接口函数成员
该接口只有一个方法GetEnumerator()
IEnumerable和 IEnumberator使用范例
一个具有IEnumberable的类
Class ColorEnumerator:IEnumerator { string [] _colors; ; //构造函数,把传入数组保存到_colors中 public ColorEnumerator(string[] theColors) { _colors=new string[thisColors.Length]; ;i<theColors.Length;i++) { _colors[i]=theColors[i]; } } //实现接口 public object Current { get { //异常处理 ||_position>=_color.Length) throw new InvalidOperationException(); return _colors[_position]; } } public bool MoveNext() { ) { _position++;return true; }else return false; } public void Reset() { _position=-; } } //继承自定义枚举器 Class Mycolors: ColorEnumerator { string[] Colors={"Red","Yellow","Blue"}; //实现IEnumerable接口返回Colors数组的迭代器类(实现了IEnumerator的类) //这个迭代器是自定义的 public IEnumbertor GetEnumbertor() { return new ColorEnumerator(Colors); } } class Program { static void Main() { Mycolors colors=new Mycolors(); foreach(string c in colors) { Console.WriteLine(c); } } }
泛型枚举接口
最主要区别:
泛型:
IEnumerable GetEnumerator获得的IEnumator是T类型的,IEnumator<T>用Current获得的不是object了,而是T类型的
IEnumerator<T>反编译
public interface IEnumerator<out T> : IDisposable, IEnumerator { T Current { get; } }
publicinterfaceIEnumerable<out T>:IEnumerable { IEnumerator<T>GetEnumerator(); }
IEnumerator非泛型类返回值已经定死为object
如果要实现泛型,则把IEnumerator非泛型放在IEnumerator泛型中,它有一个Current,这个能把数据变成指定类型。
IEnumerator/IEnumerable接口的更多相关文章
- foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- 你可能不知道的陷阱, IEnumerable接口
1. IEnumerable 与 IEnumerator IEnumerable枚举器接口的重要性,说一万句话都不过分.几乎所有集合都实现了这个接口,Linq的核心也依赖于这个万能的接口.C语言的 ...
- IEnumerable接口
IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...
- IEnumerable接口的实现
对象要实现可以迭代需IEnumerable接口并实现GetEnumerator方法.一下简单例子 public class SPEnumerable<T> : IEnumerable { ...
- IEnumerable 接口 实现foreach 遍历 实例
额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答: 该类实现IEnumetable 接口 声明 ...
- 通过IEnumerable接口遍历数据
使用IEnumerable接口遍历数据,这在项目中会经常的用到,这个类型呢主要是一个枚举器. 1.首先需要让该类型实现一个名字叫IEnumerable的接口,实现该接口的主要目的是为了让当前类型中增加 ...
- 迭代器模式 与 C# IEnumerator/IEnumerable
Part1 迭代器模式 与 接口 IEnumerable IEnumerator interface IEnumerable { IEnumerator GetEnumerator(); } // 泛 ...
- 【C#】IEnumrator的枚举数和IEnumerable接口
声明IEnumerator的枚举数 要创建非泛型接口的枚举数,必须声明实现IEnumerator接口的类,IEnumerator接口有如下特性: 1.她是System.Collections命名空间的 ...
随机推荐
- jsp中页面间传汉字参数转码
转码:a.href="./showCont.jsp?tcontent="+encodeURI(encodeURI(tcontent)); 解码:java.net.URLDecode ...
- MVC自定义错误页404静态页
昨天公司要求给所有项目添加自定义404错误页,具体的要求实现的有以下几点: 1.实现自定义错误(如各种error,404等)跳转到指定的页面 2.所指定的页面输出的http状态值必须是404或其他指定 ...
- .net 调用webservice 总结
最近做一个项目,由于是在别人框架里开发app,导致了很多限制,其中一个就是不能直接引用webservice . 我们都知道,调用webserivice 最简单的方法就是在 "引用" ...
- DTCMS使用ajax局部刷新
动力启航的DTCMS代码遇到的问题: 前台post请求: $.ajax({ type: "POST", url: sendUrl, dataType: "json&quo ...
- 基于python yield机制的异步操作同步化编程模型
又一个milestone即将结束,有了些许的时间总结研发过程中的点滴心得,今天总结下如何在编写python代码时对异步操作进行同步化模拟,从而提高代码的可读性和可扩展性. 游戏引擎一般都采用分布式框架 ...
- 两对整数明明完全一样,为何一个输出true,一个输出false?&&神奇代码(StrangeIntegerBehavior.java)输出诡异的结果,原因何在
下面有一段代码: public class Main { public static void main(String[] args) { Integer ...
- java 自动装箱和自动拆箱
自动装箱 java执行Integer i = 100:编译器编译成Integer i = Integer.valueOf(100); Integer i = 100; //编译器编译成Integer ...
- POJ 3624
背包问题,在定容量的背包中放入物体求装满价值最大.因为每种物体数量只有1,故只有放与不放. #include<iostream> #include<cstring> #incl ...
- Tables for condition techniques
T682i -- Access sequence and the tables T685 -- Condition types and Access sequences T683s -- ...
- 这是html5中WebGL的演示
这是html5中WebGL的演示,让我们与他人分享爱您发送短消息.每次你进入它使用不同的位置,新的爱情点被添加到全球.让世界更明亮的地方与你的朋友分享! 源文件:部分代码:<!DOCTYPE h ...