实现Foreach遍历
实现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遍历的更多相关文章
- 用<forEach>遍历list集合时,提示我找不到对象的属性
<c:forEach items="${list}" var="item"> <tr> <td>${item.UserId} ...
- Foreach遍历
前天在项目中遇到一个问题,foreach遍历过程中修改responses中的对象,其中responses的类型:IEnumerable<Order>,代码如下: foreach (Orde ...
- 使用yield关键字让自定义集合实现foreach遍历
一般来说当我们创建自定义集合的时候为了让其能支持foreach遍历,就只能让其实现IEnumerable接口(可能还要实现IEnumerator接口) 但是我们也可以通过使用yield关键字构建的迭代 ...
- js中三个对数组操作的函数 indexOf()方法 filter筛选 forEach遍历 map遍历
indexOf()方法 indexOf()方法返回在该数组中第一个找到的元素位置,如果它不存在则返回-1. 不使用indexOf时 var arr = ['apple','orange','pea ...
- mybatis map foreach遍历
mybatis map foreach遍历 转至http://www.cnblogs.com/yg_zhang/p/4314602.html mybatis 遍历map实例 map 数据如下 Map& ...
- foreach遍历遇到的一个细节问题
1.Invalid argument supplied for foreach()警告错误解决办法:foreach遍历之前添加is_array()判断
- IEnumerable 接口 实现foreach 遍历 实例
额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答: 该类实现IEnumetable 接口 声明 ...
- foreach遍历扩展(二)
一.前言 假设存在一个数组,其遍历模式是根据索引进行遍历的:又假设存在一个HashTable,其遍历模式是根据键值进行遍历的:无论哪种集合,如果它们的遍历没有一个共同的接口,那么在客户端进行调用的时候 ...
- c#--foreach遍历的用法与split的用法
一. foreach循环用于列举出集合中所有的元素,foreach语句中的表达式由关键字in隔开的两个项组成.in右边的项是集合名,in左边的项是变量名,用来存放该集合中的每个元素. 该循环 ...
随机推荐
- 如何更改c#项目的App.config文件
动态修改App.Config 和web.Config 首先假设你的应用程序配置文件如下: <?xml version="1.0" encoding="utf-8&q ...
- MVC4学习过程记录
终于决定开始尝试Web开发,即是为了工作也是为了自己的兴趣,决定还是从MS的MVC4开始. 首先从Asp.Net MVC4入门指南这个系列开始学习(http://www.cnblogs.com/pow ...
- Spring连接数据库的几种常用的方式
本文简单的讲解使用Spring连接数据库的几种常用方法: 测试主类为: package myspring2; import java.sql.*; import javax.sql.DataSourc ...
- 权限检查联系人ProfileProvider
每日一贴,今天的内容关键字为权限检查 ProfileProvider继承自AbstractContyactsProvider. 源代码请自行下载 每日一道理 书籍好比一架梯子,它能引领人们登上 ...
- MySQL安装详解(V5.5 For Windows)
前言 这几年一直在用MySQL,并且是Windows+.Net+MySQL的搭配,用MyISAM引擎支持过单表每天千万以上的数据递增,TB级的数据MySQL游刃有余.最近在做一个较大并发的项目,尝试了 ...
- Nagios在Ubuntu server上的安装配置
首先我参看的是Nagios的官方文档,Nagios – Installing Nagios Core From Source——The Industry Standard in IT Infrastr ...
- MYSQL 备份工具
backup of a database is a very important thing. If no backup, meet the following situation goes craz ...
- C#_deepCopy
using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Run ...
- 一般php创建的文件默认不是utf-8格式的,在网上搜罗的解决办法如下:
1.PHP本身是无编码的,所有的字符串通常都视为二进制流.因此只需要输入的字符串为Utf-8即可.若字符串采用其他编码,可以使用iconv系列函数转换编码. 2.注$content = iconv(& ...
- JSONP(处理跨域问题)
Ajax直接请求普通文件存在跨域无权限访问的问题 凡是拥有"src"这个属性的标签都拥有跨域的能力,比如<script>.<img>.<iframe& ...