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 ...
随机推荐
- UIEdgeInsetsMake, CGRectOffset等API参数详解
1, UIEdgeInsetsMake ( CGFloat top, CGFloat left, CGFloat bottom, CGFloat right ) 2,position点是相对suerL ...
- size()弃用
size() 方法在 jQuery 版本 1.8 中被废弃. 请使用 length 属性代替.
- C#实例
输出 Console.WriteLine("Hello, World!"); //输出Hello, World! 输出输入的内容 string strName; //声明一个str ...
- qt 2 打开文件选择框
QString sFileName = ""; sFileName = QFileDialog::getOpenFileName(this,tr("Open") ...
- 【leetcode❤python】232. Implement Queue using Stacks
#-*- coding: UTF-8 -*-#双栈法class Queue(object): def __init__(self): """ ...
- 【leetcode❤python】206. Reverse Linked List
# Definition for singly-linked list.# class ListNode(object):# def __init__(self, x):# s ...
- BZOJ 2658 小蓝的好友
题目链接:http://www.lydsy.com:808/JudgeOnline/problem.php?id=2658 题意:给出一个n*m的格子.某些格子中有障碍.求包含至少一个障碍的矩形有多少 ...
- Using Pre-Form Trigger In Oracle Forms
Pre-Form trigger in Oracle Forms fires during the form start-up, before forms navigates to the first ...
- Strategy策略模式
策略模式定义了一系列算法,把它们一个个封装起来,并且使它们可相互替换.该模式可使得算法能独立于使用它的客户而变化.Strategy模式是行为模式,正因为他是一种行为模式,所以他不是用来解决类的实例化的 ...
- x名称空间
XAML代码的WPF程序都需要通过语句:xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml",x就是用来映射xmlns:x= ...