用C#实现一个类的IEnumerable接口时有两种方法:1)实现非泛型IEnumerable接口;2)实现泛型IEnumerable(T)接口。如果采用方法1,当集合元素T是值类型时,将涉及到巨多的boxing和unboxing操作。因此,理所当然地采用方法2;

例如,以下代码采用方法2实现枚举从指定偏移开始所有整数

using System.Collections.Generic;
class Ints : IEnumerable<int>
{
private readonly int offset;
public Ints(int o) { offset = o; }
public IEnumerator<int> GetEnumerator()
 {
int i = offset;
while( true ) yield return i++;
}
} 编译时产生如下错误: error CS0535: 'Ints' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()' 而我们想要实现的是泛型的IEnumerable而不是非泛型的IEnumerable接口!怎么办呢? 查阅MSDN在线文档可知:泛型IEnumerable继承自非泛型IEnumerable。所有,在上述代码中加入:
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
{
  return GetEnumerator();
}
这样,非泛型方法转而调用泛型方法,从而不需要再去实现非泛型的IEnumerable接口了。

实现泛型IEnumerable接口的更多相关文章

  1. 你可能不知道的陷阱, IEnumerable接口

    1.  IEnumerable 与  IEnumerator IEnumerable枚举器接口的重要性,说一万句话都不过分.几乎所有集合都实现了这个接口,Linq的核心也依赖于这个万能的接口.C语言的 ...

  2. EF架构~为IEnumerable接口添加增删查等操作,原因是IEnumerable导航属性更放心

    回到目录 对EF开发来说,导航属性肯定都用过,事实上,它是由VS IDE工具根据你的数据库关系结构自动生成的外键属性,在类视图中可以看到相关属性,它是以外键表名来标识的,如果是一对多的关系,那么,它会 ...

  3. IEnumerable接口

    IEnumerable接口顾名思义就是 可枚举的,可列举的. 接口也很简单,返回一个 枚举器对象 IEnumerator . [ComVisible(true), Guid("496B0AB ...

  4. IEnumerator/IEnumerable接口

    IEnumberator函数成员 Current返回序列中当前位置项的 属性 只读属性 返回object类型 MoveNext把枚举器位置前进到集合中下一项的方法 新位置有效返回true,否则fals ...

  5. [原译]实现IEnumerable接口&理解yield关键字

    原文:[原译]实现IEnumerable接口&理解yield关键字 著作权声明:本文由http://leaver.me 翻译,欢迎转载分享.请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢 ...

  6. C# 索引器,实现IEnumerable接口的GetEnumerator()方法

    当自定义类需要实现索引时,可以在类中实现索引器. 用Table作为例子,Table由多个Row组成,Row由多个Cell组成, 我们需要实现自定义的table[0],row[0] 索引器定义格式为 [ ...

  7. foreach为什么要实现IEnumerable接口而不是直接用IEnumerator接口

    在.Net中,要想被foreach遍历,那么目标对象要实现IEnumerable或IEnumerable<T>接口,这个接口有一个方法,GetEnumerator(),返回一个IEnume ...

  8. IEnumerable接口的实现

    对象要实现可以迭代需IEnumerable接口并实现GetEnumerator方法.一下简单例子 public class SPEnumerable<T> : IEnumerable { ...

  9. IEnumerable 接口 实现foreach 遍历 实例

    额 为啥写着东西? 有次面试去,因为用到的时候特别少 所以没记住, 这个单词 怎么写! 经典的面试题: 能用foreach遍历访问的对象的要求? 答:  该类实现IEnumetable 接口   声明 ...

随机推荐

  1. Centos 安装配置iscsi

    在测试oracle rac的时候用iscsi来模拟磁阵的(真的磁阵需要多路径软件),简单的记录下 #scsi server yum install scsi-target-utils service ...

  2. (转)postgis常用函数介绍(一)

    http://blog.csdn.net/gisshixisheng/article/details/47701237 概述: 在进行地理信息系统开发的过程中,常用的空间数据库有esri的sde,po ...

  3. Python 之动态添加属性以及方法

    import types class Person(object): def __init__(self, newName, newAge): self.name = newName self.age ...

  4. centos 7 配置nginx

    安装nginx: curl -o  nginx.rpm http://nginx.org/packages/centos/7/noarch/RPMS/nginx-release-centos-7-0. ...

  5. .net core发布程序

    这里说的是,和.net core运行库一起发布,所以,目标运行系统,可以不安装.net core也能运行 1.project.json 把dependencies里面的type删除掉,后面加入&quo ...

  6. SpringMVC知识点总结一(非注解方式的处理器与映射器配置方法)

    一.SpringMVC处理请求原理图(参见以前博客) 1.  用户发送请求至前端控制器DispatcherServlet 2.  DispatcherServlet收到请求调用HandlerMappi ...

  7. js 根据数组分组动态生成table(相同项合并)

    <!doctype html public "-//w3c//dtd html 4.01 transitional//en" "http://www.w3.org/ ...

  8. git 的简单使用(2)

    一般情况下,你通常直接在文件管理器中把没用的文件删了,或者用rm命令删了: $ rm test.txt 你可以使用 git rm test.txt来删除 然后用git commit -m " ...

  9. 修改bash命令提示符

    说明:PS1是主要的提示符设置,在ubuntu一般为: ${debian_chroot:+($debian_chroot)}\u@\h:\w\$ 具体的提示符,按分类含义如下: 主要信息: \u 当前 ...

  10. BZOJ 1452 Count 【模板】二维树状数组

    对每种颜色开一个二维树状数组 #include<cstdio> #include<algorithm> using namespace std; ; ][maxn][maxn] ...