对于IEnumerable的一点理解
2024-10-15 08:19:05
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);
}
}
}
}
最新文章
- ajax无刷新删除、复制 THINKPHP
- Oracle多表连接,提高效率,性能优化 (转)
- Unity-Animator深入系列总索引
- 【56测试】【字符串】【dp】【记忆化搜索】【数论】
- windows在当前位置打开终端
- Eclipse调试时Application XXX is waiting for the debugger to attach的提示
- AIDL:Binder invocation to an incorrect interface
- 第十三章、学习 Shell Scripts 善用判断式
- iOS之苹果和百度地图的使用
- 浅析指针(pointer)与引用(reference)
- King
- CTF---隐写术入门第二题 小苹果
- 虚方法(virtual)和抽象方法(abstract)的和接口(interface)的区别
- 网络编程第三讲UDP编写
- Android JNI 传递对象
- JAVA自动补全代码
- org.apache.ibatis.binding.BindingException: Parameter &#39;idList&#39; not found解决办法
- Centos 7 安装 FFmpeg
- last命令详解
- 【五】安装fcig
热门文章
- HDU 5452 Minimum Cut
- 【模拟】NEERC15 A Adjustment Office (2015-2016 ACM-ICPC)(Codeforces GYM 100851)
- 调试中除了在URL上加时间戳外,如何避免js、css被返回304状态?
- 双有序队列算法——处理哈夫曼K叉树的高效算法
- Assumptions
- poj1038
- webservice使用基本技巧
- 你好,C++(12)怎样管理多个类型同样性质同样的数据?3.6 数组
- [转] 多线程下变量-gcc原子操作 __sync_fetch_and_add等
- 使用jQuery Mobile和Phone Gap开发Android应用程序(转)