当前位置: 首页 > news >正文

IEnumerable 和 IEnumerator 接口

实现IEnumerable接口的类,可以支持foreach循环遍历对象的集合元素

IEnumerable:
IEnumerator GetEnumerator()返回可循环访问集合的枚举数。

IEnumerator:
object Current获取集合中的当前元素。
bool MoveNext()将枚举数推进到集合的下一个元素。
如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false
void Reset()将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。


sample:
using  System;
using  System.Collections;

namespace  ConsoleApplication1
ExpandedBlockStart.gif
{
ExpandedSubBlockStart.gif    
/// <summary>
    
/// Summary description for Class1.
    
/// </summary>

    class Class1
ExpandedSubBlockStart.gif    
{
ExpandedSubBlockStart.gif        
/// <summary>
        
/// The main entry point for the application.
        
/// </summary>

        [STAThread]
        
static void Main(string[] args)
ExpandedSubBlockStart.gif        
{
            CStringEnum se 
= new CStringEnum();

            
foreach (string s in se)
ExpandedSubBlockStart.gif            
{
                Console.WriteLine(s);
            }


            Console.Read() ;
        }

    }


    
class CStringEnum : IEnumerable, IEnumerator
ExpandedSubBlockStart.gif    
{
        
string[] items = new string[16];
        
int index = -1;

        
public CStringEnum()
ExpandedSubBlockStart.gif        
{
            
for(int i=0; i<items.Length; ++i)
                items[i] 
= "s" + i.ToString();
        }


ExpandedSubBlockStart.gif        
#region IEnumerable Members

        
public IEnumerator GetEnumerator()
ExpandedSubBlockStart.gif        
{
            
// TODO:  Add StringCollection.GetEnumerator implementation
            return (IEnumerator)this;
        }


        
#endregion


ExpandedSubBlockStart.gif        
#region IEnumerator Members

        
public void Reset()
ExpandedSubBlockStart.gif        
{
            
// TODO:  Add CStringEnum.Reset implementation
            index = -1;
        }


        
public object Current
ExpandedSubBlockStart.gif        
{
            
get
ExpandedSubBlockStart.gif            
{
                
// TODO:  Add CStringEnum.Current getter implementation
                return items[index];
            }

        }


        
public bool MoveNext()
ExpandedSubBlockStart.gif        
{
            
// TODO:  Add CStringEnum.MoveNext implementation
            index++;
            
return index >= items.Length ? false : true;
        }


        
#endregion

    }


}

转载于:https://www.cnblogs.com/panjun-Donet/archive/2009/02/20/1395017.html

相关文章:

  • Ionic 入门与实战之第三章:Ionic 项目结构以及路由配置
  • 使用HBuilder编辑器进行真机调试运行时提示Waiting for debugger!
  • 交换机端口安全
  • SQLite入门与分析(四)---Page Cache之事务处理(2)
  • Windows上为Apache配置HTTPS
  • [数据恢复答疑]IBM 的RAID5E和RAID5EE适合我吗?
  • 【LeeCode】Two Sum
  • Alexnet参数解释
  • es6的常用语法
  • asp.net 点击girdView中的超链接列复制地址--源码--调试成功
  • 定制用户不使用某个帐号
  • 解读sencha touch移动框架的核心架构(一)
  • DataView(2):RowState
  • Spark学习笔记之相关记录
  • Js实现Repeater全选/反选 功能 终极解决方案
  • 2019.2.20 c++ 知识梳理
  • 3.7、@ResponseBody 和 @RestController
  • create-react-app做的留言板
  • HTTP中的ETag在移动客户端的应用
  • linux安装openssl、swoole等扩展的具体步骤
  • Linux学习笔记6-使用fdisk进行磁盘管理
  • PHP面试之三:MySQL数据库
  • Python3爬取英雄联盟英雄皮肤大图
  • Vue 动态创建 component
  • Windows Containers 大冒险: 容器网络
  • 阿里云应用高可用服务公测发布
  • 给新手的新浪微博 SDK 集成教程【一】
  • 工作中总结前端开发流程--vue项目
  • 基于Javascript, Springboot的管理系统报表查询页面代码设计
  • 猫头鹰的深夜翻译:Java 2D Graphics, 简单的仿射变换
  • 设计模式 开闭原则
  • 实现菜单下拉伸展折叠效果demo
  • 译有关态射的一切
  • hi-nginx-1.3.4编译安装
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ​力扣解法汇总1802. 有界数组中指定下标处的最大值
  • #我与Java虚拟机的故事#连载05:Java虚拟机的修炼之道
  • #在线报价接单​再坚持一下 明天是真的周六.出现货 实单来谈
  • $jQuery 重写Alert样式方法
  • (02)Hive SQL编译成MapReduce任务的过程
  • (8)STL算法之替换
  • (第8天)保姆级 PL/SQL Developer 安装与配置
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (九)c52学习之旅-定时器
  • (每日持续更新)jdk api之StringBufferInputStream基础、应用、实战
  • (转)shell调试方法
  • (转贴)用VML开发工作流设计器 UCML.NET工作流管理系统
  • ***通过什么方式***网吧
  • .bat批处理(四):路径相关%cd%和%~dp0的区别
  • .net 4.0 A potentially dangerous Request.Form value was detected from the client 的解决方案
  • .NET Core 实现 Redis 批量查询指定格式的Key
  • .NET Framework杂记
  • .NET 同步与异步 之 原子操作和自旋锁(Interlocked、SpinLock)(九)
  • .NET 中 GetHashCode 的哈希值有多大概率会相同(哈希碰撞)
  • .Net(C#)常用转换byte转uint32、byte转float等