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 ...
随机推荐
- java解析密钥格式
import java.io.StringReader; import org.bouncycastle.asn1.ASN1Sequence; import org.bouncycastle.asn1 ...
- 土豪聪要请客(stol)
土豪聪要请客(stol) 众所周知,聪哥(ndsf)是个土豪,不过你们不知道的是他的MZ和他的RMB一样滴多…… 某天土豪聪又赚了10^10000e的RMB,他比较开心,于是准备请客.他在自己在XX星 ...
- Objective之ARC
http://blog.csdn.net/siemenliu/article/details/7891345
- Web自定义协议,BS端启动CS端,
实例 1.准备CS项目,windows窗体应用程序,拖进来一个label控件来接受BS的参数,并显示,右击生成,复制该文件的bin目录下的exe,例如放在以下路径,例如C:\\simu\\下, 2.编 ...
- Threads in Spring
使用Spring时经常会问,我们定义的Bean应该是Singleton还是Prototype?多个客户端同时调用Dao层,需要考虑线程安全吗?通过阅读官方文档和Spring的源代码,这类问题的答案是: ...
- DataTable或者DataRow转换对象
public static IEnumerable<T> ConvertObject<T>(DataTable dt) where T : new() { var v = ty ...
- Highlighting Text Item On Entry In Oracle Forms
Highlight a Text Item in Oracle Forms With Visual Attribute It is very necessary to highlight the cu ...
- 权威发布:长链非编码RNA命名规则
转自:http://blog.sina.com.cn/s/blog_8088f3700101pab7.html 权威发布:长链非编码RNA命名规则 对于人类基因命名标准的制定而言,雨果基因命名委员会( ...
- 【VB6笔记-02】从Command中获取链接参数
Public Sub GetParameters() Dim Para As String Para = Command$() gstrUserID = GetCommandPara(Para, ) ...
- this call和apply
this指针总是指向一个对象,大致可以分为以下四种: 1,作为对象的方法调用(this指向该对象) 2,作为普通函数调用 当函数不作为对象的属性被调用时,也就是普通函数方式,此时的this总是指向全局 ...