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命名空间的 ...
随机推荐
- 简单的MySQL数据库主从同步配置
Master配置: 代码如下 复制代码 log-bin = mysql-binserver-id = 1binlog-do-db = powerdns #要同步的数据库 Master新增slave账号 ...
- Row_Number实现分页(适用SQL)
1:首先是 select ROW_NUMBER() over(order by id asc) as 'rowNumber', * from table1 生成带序号的集合 2:再查询该集合的 第 1 ...
- UI3_UITableView
// // AppDelegate.m // UI3_UITableView // // Created by zhangxueming on 15/7/13. // Copyright (c) 20 ...
- oracle 数据库导入导出
要把公司的数据库导入到自己的电脑上(都需要再命令窗口下输入指令) 导出数据库的基本代码: exp zj_user_kf/oracle@tzsw_4 file=d:\test3.dmp full=y 导 ...
- POJ 2407 Relatives
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 13920 Accepted: 6965 Description Give ...
- 洛谷 P1209 修理牛棚== Codevs 2079 修理牛棚
时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 在一个夜黑风高,下着暴风雨的夜晚,farmer John的牛棚的屋顶.门被吹 ...
- 修改 timezone
1.通过命令修改 1.Set Time, Date Timezone in Linux from Command Line or Gnome | Use ntp 2.Use TZ database 3 ...
- Oracle 创建用户授权
权限: create session create table unlimited tablespace connect resource dba 例: #sqlplus /nolog SQL> ...
- jquery源码分析学习地址
http://www.ccvita.com/121.htmljQuery工作原理解析以及源代码示例http://www.cnblogs.com/haogj/archive/2010/04/19/171 ...
- c#键盘鼠标钩子
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.W ...