public class Persons:IEnumerable
{
public Persons(string[] people)
{
this.people = people;
} public string[] people { set; get; }
public IEnumerator GetEnumerator()
{
return new PersonEnumable(people);
}
}
public class PersonEnumable : IEnumerator
{
public PersonEnumable(string[] people)
{
this.people = people;
}
private string[] people { set; get; }
private int index { set; get; } = -;
/// <summary>
/// 当前对象
/// </summary>
public object Current {
get {
if (index < || index > people.Length-)
return null;
else
return people[index];
}
}
/// <summary>
///
/// </summary>
/// <returns></returns>
public bool MoveNext()
{
if (index < people.Length-)
{
index++;
return true;
}
return false;
} /// <summary>
///
/// </summary>
public void Reset()
{
index = -;
}
}
var strArr = new string[] { "科比", "詹姆斯", "杜兰特", "诺维茨基", "东契奇", "卢比奥" };
var persons = new Persons(strArr);
foreach (var t in persons)
{
Console.WriteLine(t);
}
//foreachq其实大概就是执行了这段代码,只是不知道是什么时候执行的Reset()
PersonEnumable personEnumable = new PersonEnumable(strArr);
while (personEnumable.MoveNext())
{
Console.WriteLine(personEnumable.Current);
}

运行结果如下

可以看出两次输出结果是一样的。

c# 是如何对一个可遍历对象实现遍历的的更多相关文章

  1. JS 遍历对象 jQuery遍历对象

    jquery for 循环遍历对象的属性: //对象的定义如下: var person={id:"1",name:"springok",age:25}; for ...

  2. vue - for 遍历对象和遍历对象数组

    1. 遍历对象时,参数: 第一个为值,第二个为键名,第三个为索引 <!DOCTYPE html> <html lang="en"> <head> ...

  3. runtime获取类名,遍历变量,遍历对象,遍历方法

  4. JS之对象数组遍历?

    一.js实现遍历对象 <script> ","destroy":"97%"}; var props = ""; for ...

  5. PHP对象的遍历

    对象的遍历 对象的遍历,跟数组的遍历,一样! 其实,只能遍历出对象的“实例属性数据” foreach( $对象名  as   $key => $value){ //这里就可以处理$key和$va ...

  6. java 数据类型:集合接口Collection之常用ArrayList;lambda表达式遍历;iterator遍历;forEachRemaining遍历;增强for遍历;removeIf批量操作集合元素(Predicate);

    java.util.Collection接口 Java的集合主要由两个接口派生出来,一个是Collection一个是Map,本章只记录Collection常用集合 集合只能存储引用类型数据,不能存储基 ...

  7. jquery遍历对象,数组,集合

    1.jquery 遍历对象 <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTM ...

  8. JavaScript基础精华03(String对象,Array对象,循环遍历数组,JS中的Dictionary,Array的简化声明)

    String对象(*) length属性:获取字符串的字符个数.(无论中文字符还是英文字符都算1个字符.) charAt(index)方法:获取指定索引位置的字符.(索引从0开始) indexOf(‘ ...

  9. Java基础知识强化之集合框架笔记46:Set集合之TreeSet存储自定义对象并遍历练习2(自然排序:Comparable)

    1. TreeSet存储自定义对象并遍历练习2: (1)Student.java package cn.itcast_06; /* * 如果一个类的元素要想能够进行自然排序,就必须实现自然排序接口 * ...

随机推荐

  1. Qt事件机制浅析

    Qt事件机制 Qt程序是事件驱动的, 程序的每个动作都是由幕后某个事件所触发.. Qt事件的发生和处理成为程序运行的主线,存在于程序整个生命周期. Qt事件的类型很多, 常见的qt的事件如下: 键盘事 ...

  2. Failed to execute aapt

    Failed to execute aapt 没错,看到这个表示你的资源出错了.不用想别的. 比如: Failed to execute aapt com.android.ide.common.pro ...

  3. [MyBatis]再次向MySql一张表插入一千万条数据 批量插入 用时5m24s

    本例代码下载:https://files.cnblogs.com/files/xiandedanteng/InsertMillionComparison20191012.rar 环境依然和原来一样. ...

  4. elasticsearch+kibana setup

    加载示例数据,设置index的时候出错: 提示 forbidden 则可能是es的问题,需要执行如下命令: curl -XPUT -H "Content-Type: application/ ...

  5. Go项目的测试代码2(项目运用)

    上一篇文章介绍了最基本的测试代码的写法.Go项目的测试代码(基础) 这里简单的共享一下我在项目中使用的方式. 项目结构 我们实际项目中, 结构简单地分了控制层controllers和模块层models ...

  6. linux如何离线加载docker镜像?

    1. 在已经部署了镜像的机器上获取镜像 1.1 获取镜像名 docker images 1.2 打包选中对应的镜像 docker save <image_name> -o <imag ...

  7. 怎么彻底关闭卸载删除Cortana小娜进程,最简单

    原文地址:https://jingyan.baidu.com/article/90bc8fc8be67bcf653640cfa.html Win10中的Cortana是微软开发的一款个人AI助理,集聊 ...

  8. lftp下载文件无法覆盖,提示" file already existst and xfer:clobber is unset" 问题解决

    在 /etc/lftp.conf   文件中添加以下配置即可 set xfer:clobber on

  9. java配置SSM框架下的redis缓存

    pom.xml引入依赖包 <!--jedis.jar --> <dependency> <groupId>redis.clients</groupId> ...

  10. 【Linux】配置SSH免密登录

    环境说明 假设我们有三台机器分别为bigdata111,bigdata112,bigdata113,三台机器均为centos 7系统. 配置SSH免密登录 (1)利用Xshell的发送键输入到所有会话 ...