实现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左边的项是变量名,用来存放该集合中的每个元素. 该循环 ...
随机推荐
- python中objects的all和get方法的区别
all返回的是QuerySet: get返回的是模型对象. 想要获取查询结果的字段值: 从QuerySet中获取对象可以通过for in的形式遍历,之后通过对象获取对象的具体值: get 返回的是对象 ...
- PHP AJAXFORM提交图片上传并显示图片源代码
PHP dofile.php 文件上传源代码 <? php $file_upload = "upload/"; $file_allow_ext='gif|jpg|jpeg|p ...
- JellyViewPager
https://github.com/jfeinstein10/JazzyViewPager https://github.com/chiemy/JellyViewPager JellyViewPag ...
- Tomcat7集群扩展session集中管理,tomcat-redis-session-manager使用
请参考官方文档 下载所需的包了: tomcat-redis-session-manager-1.1.jar jedis-2.1.0.jar commons-pool-1.6.jar 将这些jar包都丢 ...
- Android 实现书籍翻页效果----升级篇
自从之前发布了<Android 实现书籍翻页效果----完结篇 >之后,收到了很多朋友给我留言,前段时间由于事情较多,博客写得太匆忙很多细节地方没有描述清楚.所以不少人对其中的地方有不少不 ...
- StarlingMVC Framework 原理。。。
向starlingmvc 中添加bean后..会根据Metadata标签,分别交给不同的Processor去处理...然后会执行每个bean的postConstruct函数.相当于初始化函数...可以 ...
- mydumper原理2
使用mydumper备份发生Waiting for table flush,导致所有线程都无法读和写 版本 mydumper 0.9.1OS centos6.6 X86_64mysql 5.6.25- ...
- python---pyc pyo文件详解
http://blog.csdn.net/balabalamerobert/article/details/2683029#comments http://blog.csdn.net/zhengsen ...
- placement new 操作符
placement new操作符能够在分配内存时指定内存位置.下面的程序使用了placement new操作符和常规new操作符给对象分配内存. // placenew.cpp -- new, pla ...
- NopCommerce 数据库初始化
NopCommerce数据库初始化比较复杂,我简化了,只初始化创建一张表,不多说,直接上代码: //数据实体 /// <summary> /// Represents an affilia ...