IEnumerable接口的实现
对象要实现可以迭代需IEnumerable接口并实现GetEnumerator方法。一下简单例子
public class SPEnumerable<T> : IEnumerable
{
private T[] array; public SPEnumerable()
{
array = new T[];
} public void Add(T item)
{
Array.Resize<T>(ref array, array.Length + );
array[array.Length - ] = item;
} #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator()
{
foreach (T item in array)
{
yield return item;
}
} #endregion
}
当然也可以自己去实现IEnumerator接口
public class SPEnumerable<T> : IEnumerable
{
private T[] array; public SPEnumerable()
{
array = new T[];
} public void Add(T item)
{
Array.Resize<T>(ref array, array.Length + );
array[array.Length - ] = item;
} #region IEnumerable 成员 IEnumerator IEnumerable.GetEnumerator()
{
return new SPEnumerator<T>(array);
} #endregion
} public class SPEnumerator<T> : IEnumerator
{
private int position = -;
private T[] array; public SPEnumerator(T[] array)
{
this.array = array;
} #region IEnumerator 成员 public object Current
{
get
{
return array[position];
}
} public bool MoveNext()
{
position++;
return position < array.Length;
} public void Reset()
{
position = -;
} #endregion
}
IEnumerable接口的实现的更多相关文章
- C# 索引器,实现IEnumerable接口的GetEnumerator()方法
当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...
- 你可能不知道的陷阱, IEnumerable接口
1. IEnumerable 与 IEnumerator IEnumerable枚举器接口的重要性,说一万句话都不过分.几乎所有集合都实现了这个接口,Linq的核心也依赖于这个万能的接口.C语言的 ...
- foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口
在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...
- EF架构~为IEnumerable接口添加增删查等操作,原因是IEnumerable导航属性更放心
回到目录 对EF开发来说,导航属性肯定都用过,事实上,它是由VS IDE工具根据你的数据库关系结构自动生成的外键属性,在类视图中可以看到相关属性,它是以外键表名来标识的,如果是一对多的关系,那么,它会 ...
- IEnumerable接口
IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...
- IEnumerable 接口 实现foreach 遍历 实例
额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答: 该类实现IEnumetable 接口 声明 ...
- IEnumerator/IEnumerable接口
IEnumberator函数成员 Current返回序列中当前位置项的 属性 只读属性 返回object类型 MoveNext把枚举器位置前进到集合中下一项的方法 新位置有效返回true,否则fals ...
- IEnumerable接口的扩展方法
/// <summary>/// IEnumerable接口的扩展方法,支持它的实现类是List的情况/// </summary>using System.Collection ...
- 一位有着工匠精神的博主写的关于IEnumerable接口的详细解析
在此,推荐一位有着工匠精神的博主写的一篇关于IEnumerable接口的深入解析的文章:http://www.cnblogs.com/zhaopei/p/5769782.html#autoid-0-0 ...
随机推荐
- 在HTML5规范中div中读取预存的data-[key]值
HTML 代码: <div id="div_test" data-test="this is test" ></div> jQuery ...
- 优秀的Markdown编辑器MarkdownPad2免费版使用全功能
MarkdownPad,一款不错的Markdown编辑器,本人一直在用,具备所有Markdown的基本语法外支持一些特别的扩展,比如表格等. MarkdownPad分为免费版和收费版,区别是免费版不支 ...
- unity3d中asset store 的资源下载到本地的目录位置
来源:http://blog.csdn.net/fzhlee/article/details/8613688 C:/Users/[当前用户]/AppData/Roaming/Unity/Asset S ...
- canvas背景透明
var can=document.getElementById("canv"); c=can.getContext("2d"); c.globalAlpha=. ...
- 保留ip: Reserved IP addresses
Reserved IP addresses From Wikipedia, the free encyclopedia In the Internet addressing architect ...
- Spring 框架的设计理念与设计模式分析
转载地址:https://www.ibm.com/developerworks/cn/java/j-lo-spring-principle/ Spring 作为现在最优秀的框架之一,已被广泛的使用,并 ...
- mysql 查询开销 sending data
1.执行一个查询,发现时间开销都在sending data,为什么?2.sending data容易误导,让人以为只是发送数据给客户端,实际上sending data包含两个过程:读取数据并处理,发送 ...
- Python学习笔记2—内置函数
函数的使用 官方文档:https://docs.python.org/2/library/functions.html
- JS---------->数组练习!
var arr = [4, 0, 7, 9, 0, 0, 2, 6, 0, 3, 1, 0]; 要求将数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组 <!doctype htm ...
- 【linux 命令】:查看系统开机,关机时间【转载】
转载原文:http://www.cnblogs.com/kerrycode/p/3759395.html 看Linux开机关机时间的方法(非常全面) 1: who 命令查看 who -b 查看最后一次 ...