一、 IEnumerator

解释:它是一个的集合访问器,使用foreach语句遍历集合或数组时,就是调用 Current、MoveNext()的结果。

  1. // 定义如下
    public interface IEnumerator
  2. {
  3. // 返回结果: 集合中的当前元素。
  4. object Current { get; }
  5.  
  6. // 返回结果: 如果枚举数成功地推进到下一个元素,则为 true;如果枚举数越过集合的结尾,则为 false。
  7. bool MoveNext();
  8.  
  9. // 调用结果:将枚举数设置为其初始位置,该位置位于集合中第一个元素之前。
  10. void Reset();
  11. }

二、IEnumerable

解释:它利用 GetEnumerator() 返回 IEnumerator 集合访问器。

  1. // 定义如下
  2. public interface IEnumerable
  3. {
  4. // 返回结果: 可用于循环访问集合的IEnumerator 对象。
  5. IEnumerator GetEnumerator();
  6. }

三、举个栗子

  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4.  
  5. namespace ArrayListToList
  6. {
  7. // 定义student类
  8. public class Student
  9. {
  10. public string Id { get; set; }
  11.  
  12. public string Name { get; set; }
  13.  
  14. public string Remarks { get; set; }
  15.  
  16. public Student(string id, string name, string remarks)
  17. {
  18. this.Id = id;
  19. this.Name = name;
  20. this.Remarks = remarks;
  21. }
  22. }
  23.  
  24. class Program
  25. {
  26. static void Main(string[] args)
  27. {
  28.  
  29. ArrayList arrStus = new ArrayList
  30. {
  31. new Student("", "liuliu"," little rabbit"),
  32. new Student("", "zhangsan", "little tortoise")
  33. };
  34. // List<T> 继承了IEnumerable<T>, IEnumerble<T>继承了IEnumerable.
  35. List<Student> stuL = ArrListToArr<Student>(arrStus);
  36. foreach(Student stu in stuL)
  37. {
  38. Console.WriteLine($"{ stu.Name + " " + stu.Id + " " + stu.Remarks }");
  39. };
  40. }
  41.  
  42. // arrList 转换为 List<T>
  43. // ArrList 定义时已继承了IEnumerable
  44. static List<T> ArrListToArr<T>(ArrayList arrL)
  45. {
  46. List<T> list = new List<T>();
  47.  
  48. IEnumerator enumerator = arrL.GetEnumerator();
  49.  
  50. while (enumerator.MoveNext())
  51. {
  52. T item = (T)(enumerator.Current);
  53. list.Add(item);
  54. }
  55.  
  56. return list;
  57. }
  58. }
  59. }

结果:

C#--IEnumerable 与 IEnumerator 的区别的更多相关文章

  1. 关于迭代器中IEnumerable与IEnumerator的区别

    首先是IEnumerable与IEnumerator的定义: 1.IEnumerable接口允许使用foreach循环,包含GetEnumerator()方法,可以迭代集合中的项. 2.IEnumer ...

  2. 转载IEnumerable与IEnumerator区别

    public interface IEnumerable {     IEnumerator GetEnumerator(); }   public interface IEnumerator {   ...

  3. C#编程之IList<T>、List<T>、ArrayList、IList, ICollection、IEnumerable、IEnumerator、IQueryable 和 IEnumerable的区别

    额...今天看了半天Ilist<T>和List<T>的区别,然后惊奇的发现使用IList<T>还是List<T>对我的项目来说没有区别...  在C#中 ...

  4. Asp.Net IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  5. 在C#中IEnumerable与IEnumerator

    对于很多刚开始学习C#同学来说经常会遇到IEnumerable这个关键字,enumerate在字典里的解释是列举,枚举,因此可想而知这个关键字肯定是和列举数据有关的操作. public interfa ...

  6. IEnumerable,ICollection,IList,List区别

    做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...

  7. C#中的 IList, ICollection ,IEnumerable 和 IEnumerator

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  8. IList, ICollection ,IEnumerable AND IEnumerator in C#

    IList, ICollection ,IEnumerable 很显然,这些都是集合接口的定义,先看看定义: // 摘要: // 表示可按照索引单独访问的对象的非泛型集合. [ComVisible(t ...

  9. 由IEnumerable和IEnumerator的延伸

    相信大家在学习c#的时候,经常会看到IEnumerable.IEnumerator这样的接口或返回类型,在写代码时经常会对数组或List集合进行遍历.那IEnumerable和IEnumerator是 ...

随机推荐

  1. 「SCOI2015」小凸玩矩阵 解题报告

    「SCOI2015」小凸玩矩阵 我好沙茶啊 把点当边连接行和列,在外面二分答案跑图的匹配就行了 我最开始二分方向搞反了,样例没过. 脑袋一抽,这绝壁要费用流,连忙打了个KM 然后wa了,一想这个不是完 ...

  2. Uva796 Critical Links

    用tarjan缩点 然后用dfn[u] < low[v]缩点并且保存起来 在sort一遍输出 #include<stdio.h> #include<string.h> # ...

  3. css 圆形头像

    方法一:背景图片(推荐) 好处是,图片长宽不等的情况下图片不会变形 .ui-photo { width: 100px; height: 100px; background: url("img ...

  4. HEOI2019退役总结

    真的很快,一切就都已经尘埃落定了. 其实经历不是很圆满的时候,是不想写这一类游记总结的,但这次其实不太一样,总要让这段经历有始有终. 可能会很啰嗦…… 赛前 收到了若干鼓励,包括老师的手写祝福和学长学 ...

  5. CSS解决文字超出显示省略号问题

    超出一行 white-space: nowrap; overflow: hidden; text-overflow: ellipsis; 超出多行 overflow: hidden; text-ove ...

  6. A1072. Gas Station

    A gas station has to be built at such a location that the minimum distance between the station and a ...

  7. Django 路由报错友好提示

    这个方法要在设置路由文件内使用也就是urls.py内. """mysite URL Configuration The `urlpatterns` list routes ...

  8. Django(十九)Ajax全套

    参考博客:http://www.cnblogs.com/wupeiqi/articles/5703697.html 提交: - Form - Ajax 一.Ajax,偷偷向后台发请求 - XMLHtt ...

  9. redis4.0.6集群搭建

    文件环境:CentOS7 + redis4.0.6 先去官网下载redis:https://redis.io/,然后上传到你的虚拟机,我上传到了/mysoft 先解压->然后进入主目录-> ...

  10. Good Bye 2018

    Good Bye 2018 2018年最后一场CF,OVER! 弱弱的我只能做出3道A,B,D~~~~ 最后几分钟,感觉找到了C题的规律,结束的那一刻,提交了一发 "Wrong answer ...