using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace ForeachDemo
{
class Program
{
static void Main()
{
Mylist list = new Mylist();
MyList2 list2 = new MyList2();
foreach (string str in list)
{
Console.WriteLine(str);
}
Console.WriteLine("==========================================");
foreach (string str in list2)
{
Console.WriteLine(str);
}
}
} class Mylist : IEnumerable //通过yield return 来返回,实现IEnumerable接口
{
static string[] testString= { "", "", "", "" ,""}; public IEnumerator GetEnumerator()
{
foreach (string str in testString)
{
yield return str;
}
}
} class MyList2 : IEnumerable //实现自己的IEnumerator来实现
{
static string[] testString2 = { "", "", "", "", "" };
public IEnumerator GetEnumerator()
{
return new MyEnumerator();
}
private class MyEnumerator : IEnumerator
{
int index = -;
public object Current
{
get
{
return testString2[index];
}
} public bool MoveNext()
{
if (++index >= testString2.Length)
{
return false;
}
else
{
return true;
}
} public void Reset()
{
index = -;
} }
} }

参考:

http://www.cnblogs.com/jesse2013/p/CollectionsInCSharp.html

http://www.cnblogs.com/kingcat/archive/2012/07/11/2585943.html

最新文章

  1. Node.js系列基础学习----安装,实现Hello World, REPL
  2. Ubuntu 16.04 nvidia安装
  3. 收缩 虚拟硬盘 shrink vhd
  4. POJ 2159 Ancient Cipher 难度:0
  5. C++ Code_ImageList
  6. Java中的volatile
  7. JSP路径的问题
  8. Java---练习:文件切割与合并(1)
  9. web浏览器中的javascript 1
  10. import和require
  11. 【转】【Linux】Swap与Memory
  12. C++_day06_运算符重载_智能指针
  13. page用法
  14. Zabbix 配置监控主机
  15. SQL Server 2008 开启远程连接
  16. vue购物车实战项01
  17. [jzoj]3777.最短路(shortest)
  18. Android remount命令的两种写法
  19. 为Bootstrap模态对话框添加拖拽移动功能
  20. [web前端] yarn和npm命令使用

热门文章

  1. hdoj 3342 Legal or Not【拓扑排序】
  2. CentOS 7 安装和配置JDK
  3. RabbitMQ 概念
  4. 帮助招聘程序员的自动考试网站:Codility
  5. 使用redis来实现分布式锁
  6. springboot +spring security4 +thymeleaf 后台管理系统
  7. Nunit中文文档
  8. android 31 GridView
  9. android 17 activity生命周期
  10. 关于怎么将Quartus和Nios程序一起固化到FPGA里面