IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器,这两个接口对于LINQ的理解是非常重要的。

  在网上看了些这方面的东西,在此对自己看到的一些这方面知识进行下总结。废话不多说,直接上代码。

namespace IEnumeratorSample
{
using System;
using System.Collections;
using System.Collections.Generic; internal class Person : IEnumerable<PersonEnum>
{
private ICollection<PersonEnum> PersonList { get; set; } public Person()
{
this.PersonList = new List<PersonEnum>();
} public IEnumerator GetEnumerator()//实现接口
{
return this.PersonList.GetEnumerator();
} public void AddItem(PersonEnum entity)
{
if (PersonList == null)
{
PersonList = new List<PersonEnum>();
}
PersonList.Add(entity);
} public bool Remove(PersonEnum entity)
{
return PersonList.Remove(entity);
} IEnumerator<PersonEnum> IEnumerable<PersonEnum>.GetEnumerator()
{
return this.PersonList.GetEnumerator();
}
} internal class PersonEnum
{
public string Name; public string Age; public PersonEnum(string name, string age)
{
Name = name; Age = age;
}
} internal class Program
{
private static void Main(string[] args)
{
PersonEnum one = new PersonEnum("leo", "");
PersonEnum two = new PersonEnum("Ray", "");
Person persons = new Person();
persons.AddItem(one);
persons.AddItem(two);
foreach (PersonEnum person in persons)
{
Console.WriteLine(person.Name);
}
}
}
}

最新文章

  1. ajax无刷新删除、复制 THINKPHP
  2. Oracle多表连接,提高效率,性能优化 (转)
  3. Unity-Animator深入系列总索引
  4. 【56测试】【字符串】【dp】【记忆化搜索】【数论】
  5. windows在当前位置打开终端
  6. Eclipse调试时Application XXX is waiting for the debugger to attach的提示
  7. AIDL:Binder invocation to an incorrect interface
  8. 第十三章、学习 Shell Scripts 善用判断式
  9. iOS之苹果和百度地图的使用
  10. 浅析指针(pointer)与引用(reference)
  11. King
  12. CTF---隐写术入门第二题 小苹果
  13. 虚方法(virtual)和抽象方法(abstract)的和接口(interface)的区别
  14. 网络编程第三讲UDP编写
  15. Android JNI 传递对象
  16. JAVA自动补全代码
  17. org.apache.ibatis.binding.BindingException: Parameter &#39;idList&#39; not found解决办法
  18. Centos 7 安装 FFmpeg
  19. last命令详解
  20. 【五】安装fcig

热门文章

  1. HDU 5452 Minimum Cut
  2. 【模拟】NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)
  3. 调试中除了在URL上加时间戳外,如何避免js、css被返回304状态?
  4. 双有序队列算法——处理哈夫曼K叉树的高效算法
  5. Assumptions
  6. poj1038
  7. webservice使用基本技巧
  8. 你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
  9. [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等
  10. 使用jQuery Mobile和Phone Gap开发Android应用程序(转)