实现Foreach遍历的集合类,需要实现IEnumerable接口,泛型集合则需要实现IEnumerable<T>接口

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Foreach
{
public class ListForeach<T> :IEnumerable<T> where T :class ,new()
{
private T[] array; private int index = ;
public ListForeach(int capcity)
{
array = new T[capcity];
} public int Count
{
get
{
return index;
}
} public void Add(T value)
{
if (index >= array.Length)
{
T[] newArr = new T[array.Length * ];
//将原来的数组中的数据拷贝到新数组中.
Array.Copy(array, newArr, array.Length);
//然后将旧数组的变量指向新数组
array = newArr;
}
array[index++] = value;
} public IEnumerator<T> GetEnumerator()
{
return new TEnumeratro<T>(array, index); } IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
} public class TEnumeratro<T> : IEnumerator<T>
{ private T [] arr ;
private int count;
private int position = -; public TEnumeratro(T [] array, int count)
{
this.arr = array;
this.count = count;
} public T Current
{
get {
return arr[position];
}
} public void Dispose()
{
arr = null;
} object IEnumerator.Current
{
get {
return this.Current;
}
} public bool MoveNext()
{
position++;
if (position < this.count)
return true;
else
return false;
} public void Reset()
{
this.position = -;
}
}
}

客户端代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text; namespace Foreach
{ public class Student
{
public int Age {get;set; }
public string Name { get; set; } } class Program
{
static void Main(string[] args)
{
ListForeach<Student> list = new ListForeach<Student>();
list.Add(new Student() {Age=,Name="" });
list.Add(new Student() { Age = , Name = "" });
list.Add(new Student() { Age = , Name = "" });
list.Add(new Student() { Age = , Name = "" });
list.Add(new Student() { Age = , Name = "" });
list.Add(new Student() { Age = , Name = "" }); foreach (var s in list)
{
Console.WriteLine(s.Name+":"+ s.Age); } ListForeach<Student> list1 = new ListForeach<Student>() {
new Student(){ Age=,Name="yjq"},
new Student(){ Age=,Name="dddd"},
new Student(){ Age=,Name="ddd"},
new Student(){ Age=,Name="==="},
}; foreach (var s in list1)
{
Console.WriteLine(s.Name + ":" + s.Age); } Console.Write(list.Count);
Console.Read(); }
}
}

实现Foreach遍历的更多相关文章

  1. 用<forEach>遍历list集合时,提示我找不到对象的属性

    <c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...

  2. Foreach遍历

    前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...

  3. 使用yield关键字让自定义集合实现foreach遍历

    一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...

  4. js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历

     indexOf()方法  indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...

  5. mybatis map foreach遍历

    mybatis map foreach遍历 转至http://www.cnblogs.com/yg_zhang/p/4314602.html mybatis 遍历map实例 map 数据如下 Map& ...

  6. foreach遍历遇到的一个细节问题

    1.Invalid argument supplied for foreach()警告错误解决办法:foreach遍历之前添加is_array()判断

  7. IEnumerable 接口 实现foreach 遍历 实例

    额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答:  该类实现IEnumetable 接口   声明 ...

  8. foreach遍历扩展(二)

    一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...

  9. c#--foreach遍历的用法与split的用法

    一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素.      该循环 ...

随机推荐

  1. Rstudio编辑界面美化设置

    美化Rstudio的编辑界面有利于我们输入代码,合适的调整更是减少错误. 可以根据自己的喜好和习惯选择.

  2. protoc-gen-lua

    lua里使用proto buffer protoc-gen-lua 官方不维护了,自己维护个:protoc-gen-lua int64支持,将64位int转换成lua的string. message相 ...

  3. hdu 2553 N皇后问题 (经典DFS)

    题目链接:点击链接 思路:用一维数组hang[num] = i,num表示第num行,i表示第i列,计算n = 1~10皇后的不同放置数量,然后打表 #include<stdio.h> # ...

  4. Xdebug的使用

    1.http://www.cnblogs.com/mo-beifeng/articles/2446142.html 2.http://www.cnblogs.com/ximu/articles/200 ...

  5. Struts2常量的具体用法实例(一)

    XML代码: <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE struts PUBLIC ...

  6. 【转】python中的lambda函数

    http://www.cnblogs.com/coderzh/archive/2010/04/30/python-cookbook-lambda.html lambda函数也叫匿名函数,即,函数没有具 ...

  7. cdoj 1250 喵哈哈的矩阵 数学题

    喵哈哈的矩阵 Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.uestc.edu.cn/#/problem/show/1250 Desc ...

  8. BZOJ 3931: [CQOI2015]网络吞吐量 最大流

    3931: [CQOI2015]网络吞吐量 Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://www.lydsy.com/JudgeOnline/p ...

  9. cocos2dx3.2 画图方法小修改之 C++ final学习

    今天用cocos2dx 3.2版本号学习画图功能,       于是我重载Node 的draw方法.发现报错, watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQ ...

  10. gcc中不同namespace中同名class冲突时

    正常情况下,编译器都会报错,提示你有两个候选类,让你明确的选择一个. 比如我的情况,我自己设计了一个类Message, 然后在某个文件里面引用了它.但是我的文件中又引入了mongodb的头文件,非常不 ...