1. class BookEnum : IEnumerator //实现foreach语句内部,并派生
  2. {
  3.  
  4. public Book[] _book; //实现数组
  5.  
  6. int position = -;//设置“指针”
  7.  
  8. public BookEnum(Book[] list)
  9. {
  10.  
  11. _book = list; //实现list
  12.  
  13. }
  14.  
  15. public bool MoveNext()//实现向前移动
  16. {
  17.  
  18. position++; //位置增加
  19.  
  20. return (position < _book.Length); //返回布尔值
  21.  
  22. }
  23.  
  24. public void Reset() //位置重置
  25. {
  26.  
  27. position = -;
  28.  
  29. }//重置指针为-1
  30.  
  31. public object Current //实现接口方法
  32. {
  33.  
  34. get
  35. {
  36.  
  37. try
  38. {
  39.  
  40. return _book[position]; //返回对象
  41.  
  42. }
  43.  
  44. catch (IndexOutOfRangeException) //捕获异常
  45. {
  46.  
  47. throw new InvalidOperationException(); //抛出异常信息
  48.  
  49. }
  50.  
  51. }
  52.  
  53. }
  54.  
  55. }
  56.  
  57. public class Book : IEnumerable
  58. {
  59.  
  60. public string Name;
  61.  
  62. public string Price;
  63.  
  64. public Book(string name, string price)
  65. {
  66.  
  67. Name = name;
  68.  
  69. Price = price;
  70.  
  71. }
  72.  
  73. private Book[] book;
  74.  
  75. public Book(Book[] array) //
  76. {
  77.  
  78. book = array;
  79.  
  80. }
  81.  
  82. public IEnumerator GetEnumerator() //实现接口
  83. {
  84.  
  85. return new BookEnum(book); //返回方法
  86. }
  87. }
  88.  
  89. public partial class MainWindow : Window
  90. {
  91. public Student stu;
  92. public MainWindow()
  93. {
  94. InitializeComponent();
  95. Book[] book = new Book[]
  96. {
  97.  
  98. new Book("水浒",""),
  99.  
  100. new Book("三国",""),
  101.  
  102. new Book("西游","")
  103.  
  104. };
  105.  
  106. foreach (Book p in book)
  107. {
  108.  
  109. this.textBox2.Text+="书的名字:" + p.Name + ";价格: " + p.Price;
  110.  
  111. }
  112.  
  113. }

实现IEnumberable接口和IEnumberator的更多相关文章

  1. IQueryable 与 IEnumberable 接口的区别

    IQueryable 与 IEnumberable 接口的区别是: IEnumberable<T> 泛型类在调用自己的 SKip 和 Take 等扩展方法之前数据就已经加载在本地内存里了, ...

  2. C# 通过IEnumberable接口和IEnumerator接口实现自定义集合类型foreach功能

    1.IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环 ...

  3. C# 通过IEnumberable接口和IEnumerator接口实现泛型和非泛型自定义集合类型foreach功能

    IEnumerator和IEnumerable的作用 其实IEnumerator和IEnumerable的作用很简单,就是让除数组和集合之外的类型也能支持foreach循环,至于foreach循环,如 ...

  4. IQueryable与IEnumberable的区别

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  5. EF入门 IQueryable和IEnumberable的区别

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  6. Entity Framework快速入门--IQueryable与IEnumberable的区别(转载)

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  7. IQueryable与IEnumberable的区别(转)

    转自 http://www.cnblogs.com/fly_dragon/archive/2011/02/21/1959933.html IEnumerable接口 公开枚举器,该枚举器支持在指定类型 ...

  8. Entity Framework快速入门--IQueryable与IEnumberable的区别

    IEnumerable接口 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代.也就是说:实现了此接口的object,就可以直接使用foreach遍历此object: IQueryable 接口 ...

  9. IQueryable & IEnumberable 区别

    Namespace And Inheritances Relations ? 1 2 3 4 5 6 7 8 9 Namespace: System.Collections     [ComVisib ...

随机推荐

  1. swipejs的bug

    Github:https://github.com/thebird/Swipe 以下bug的修复方式皆来自于网上. 现在最新的版本是2.0,bug如下: 1.触摸后不会自动播放 修复方式, funct ...

  2. hd1496---->这道题是水水的数论吗?

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1496 题意: Consider equations having the following form: ...

  3. yii2-获取配置选项的值

    Yii::$app->属性值 e.g:echo Yii::$app->id #输出basic config: $config = [ 'id' => 'basic', 'basePa ...

  4. php笔记[2]

    strlen()函数获得字符窜的长度 读取文件:fgets(),fgetss()和fgetcsv() 读取整个文件:readfile(),fpassthru()和file() 读取一个字符:fgetc ...

  5. 跨代的对决 英特尔i7-6700HQ对比i7-4720HQ性能测试

    http://itianti.sinaapp.com/index.php/cpu 跨代的对决 英特尔i7-6700HQ对比i7-4720HQ性能测试 2015-10-13 19:46:31 来源:电脑 ...

  6. Oracle PLSQL语句实例

    /** * plsql:某个项目对效率要求比较高的时候用,一般不用,大多数项目写的是夸数据库平台的,用不上. * pssql大多数能做的东西,java都能替代它.在某些特殊的地方要求用plsql的时候 ...

  7. struts2 传递数组、List、Map

    struts2 传递数组.List.Map jsp文件 数组:     <s:textfield name="ages" value="a1">&l ...

  8. linux rwxrwxrwt文件夹属性

    /tmp 的permission是rwxrwxrwt chmod 0777 /abc       rwxrwxrwx chmod  777 /abc        rwxrwxrwx chmod 17 ...

  9. 使用fragment兼容低版本的写法

      [1]定义fragment继承V4包中的Fragment    [2]定义的activity要继承v4包中的FragmentActivity   [3]通过这个方法getSupportFragme ...

  10. Erlang安装笔记

    今天,为了安装RabbitMQ,需要安装Erlang,中间遇到了一些坑,记录下来. 1. 下载Erlang安装包 http://www.erlang.org/downloads http://erla ...