实现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. Vieta定理

    一元$n$次方程$$P(x)=a_{n}x^{n}+a_{n-1}x^{n-1}+\cdots+a_{a}x+a_{0}=a_{n}(x-x_{1})(x-x_{2})\cdots (x-x_{n}) ...

  2. AndroidCharts为折线图表添加y坐标

    AndroidCharts 是一款轻量级的图表显示控件,对比起Android-Charts和AChartEngine来说简单和活泼了很多,适合数据展示不需要太过详细专业的场合,它支持简单且带动画的折线 ...

  3. 在WebClient中使用post[发送数据]

    很多时候,我们需要使用C#中的WebClient 来收发数据,WebClient 类提供向 URI 标识的任何本地.Intranet 或 Internet 资源发送数据以及从这些资源接收数据的公共方法 ...

  4. c# windowsForm打印

    在windows应用程序中文档的打印是一项非常重要的功能,在以前一直是一个非常复杂的工作,Microsoft .net Framework的打 印功能都以组件的方式提供,为程序员提供了很大的方便,但是 ...

  5. [AngularJS] Build Your Own ng-controller Directive

    /** * Created by Answer1215 on 12/21/2014. */ angular.module('app', []) .controller('FirstCtrl' , fu ...

  6. ORACLE触发器具体解释

    ORACLE PL/SQL编程之八: 把触发器说透 本篇主要内容例如以下: 8.1 触发器类型 8.1.1 DML触发器 8.1.2 替代触发器 8.1.3 系统触发器 8.2 创建触发器 8.2.1 ...

  7. HDFS原理讲解

    简介 本文是笔者在学习HDFS的时候的学习笔记整理, 将HDFS的核心功能的原理都整理在这里了. [广告] 如果你喜欢本博客,请点此查看本博客所有文章:http://www.cnblogs.com/x ...

  8. 空间的配置和释放 std::alloc

    看完了对象的构造行为和内存释放前的对象的析构行为,我们现在来看看内存的配置和释放. 对象构造前的空间分配和析构后的空间释放,定义在头文件<stl_alloc.h>中.其设计思想是: 向sy ...

  9. day05 Java基础

    1.数组初始化:为数组开辟内存空间,并为每个数组元素赋予值.数组初始化方式: 方式一:动态初始化:初始化时只指定数组长度,由系统为数组分配初始值. 格式:数组类型[] 数组名称=new 数组类型[数组 ...

  10. QPixmap 和 HBITMAP互转

    Q_GUI_EXPORT HBITMAP qt_pixmapToWinHBITMAP(const QPixmap &p, int hbitmapFormat = 0); 声明这一句后, 就可以 ...