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 ...
随机推荐
- AfterEffects 关键帧辅助功能
http://www.cocoachina.com/design/20160121/15073.html 标准缓动曲线查询网址:http://easings.net/zh-cn
- fffffffffff
create proc partPage114 @n int,--每页数量 @page int, --页码从0开始 @Mainkey int as declare @sql nvarchar(1000 ...
- fastboot 教程
参考: http://blog.csdn.net/geniusmen/article/details/7892398 http://www.cnblogs.com/eastnapoleon/p/327 ...
- C语言中的结构体,结构体数组
C语言中的结构体是一个小难点,下面我们详细来讲一下:至于什么是结构体,结构体为什么会产生,我就不说了,原因很简单,但是要注意到是结构体也是连续存储的,但要注意的是结构体里面类型各异,所以必然会产生内存 ...
- _in、_out。。。
_in 输入参数_out 输出参数_opt 参数是可选的,就是可以为NULL_ecount 所指向的缓存的元素个素 也就是括号里的数字
- java导出cvs文件
package testcvs; import java.io.BufferedWriter;import java.io.File;import java.io.FileOutputStream;i ...
- mysql 用sql语句查询一个表中的所有字段类型、注释
SELECT column_name,column_comment,data_type FROM information_schema.columns WHERE table_name='表名' AN ...
- MyBatis环境搭建
什么是MyBatis: MyBatis 是支持定制化 SQL.存储过程以及高级映射的优秀的持久层框架(O object R relatoin M mapping 框架),MyBatis 避免了几 乎所 ...
- JAVA题目
1.在项目中创建Number类,判断字符串"mingrikejijiavabu"中字符"i"出现了几次,并将结果输出. 方法一: public class Nu ...
- ALV详解:OO ALV
声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...