IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用
IEnumerable和IEnumerable<T>接口在.NET中是非常重要的接口,它允许开发人员定义foreach语句功能的实现并支持非泛型方法的简单的迭代,IEnumerable和IEnumerable<T>接口是.NET Framework中最基本的集合访问器。它定义了一组扩展方法,用来对数据集合中的元素进行遍历、过滤、排序、搜索等操作。
IEnumerable接口是非常的简单,只包含一个抽象的方法GetEnumerator(),它返回一个可用于循环访问集合的IEnumerator对象。IEnumerator对象有什么呢?它是一个真正的集合访器,没有它,就不能使用foreach语句遍历集合或数组,因为只有IEnumerator对象才能访问集合中的项,假如连集合中的项都访问不了,那么进行集合的循环遍历是不可能的事情了。
1、IEnumerator接口
提供在普通集合中遍历的接口,有Current,MoveNext(),Reset(),其中Current返回的是object类型。
IEnumerator<T>:继承自IEnumerator,有Current属性,返回的是T类型。
public interface IEnumerator
{
bool MoveNext(); //将游标的内部位置向前移动
object Current{get;} //获取当前的项(只读属性)
void Reset(); //将游标重置到第一个成员前面
}
2、IEnumerable接口
暴露一个IEnumerator,支持在普通集合中的遍历。
IEnumerable<T>:继承自IEnumerable,暴露一个IEnumerator<T>,支持在泛型集合中遍历。
// 摘要:
// 公开枚举器,该枚举器支持在指定类型的集合上进行简单迭代。
//
// 类型参数:
// T:
// 要枚举的对象的类型。
[TypeDependency("System.SZArrayHelper")]
public interface IEnumerable<out T> : IEnumerable
{
// 摘要:
// 返回一个循环访问集合的枚举器。
//
// 返回结果:
// 可用于循环访问集合的 System.Collections.Generic.IEnumerator<T>。
IEnumerator<T> GetEnumerator();
}
可以看到,GetEnumerator方法返回对另一个接口System.Collections.IEnumerator的引用。这个接口提供了基础设施,调用方可以用来移动IEnumerable兼容容器包含的内部对象。
3、ICollection接口
同时继承IEnumerable<T>和IEnumerable两个接口
// 摘要:
// 定义操作泛型集合的方法。
//
// 类型参数:
// T:
// 集合中元素的类型。
[TypeDependency("System.SZArrayHelper")]
public interface ICollection<T> : IEnumerable<T>, IEnumerable
4、IList接口
public interface IList<T> : ICollection<T>, IEnumerable<T>, IEnumerable
5、List的定义
public class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable
实现IEnumerable接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
namespace IEnumeratorSample
{
class Person : IEnumerable
{
public string Name;//定义Person的名字
public string Age;//定义Person的年龄 public Person(string name, string age)//为Person初始化(构造函数)
{
Name = name;
Age = age;
} private Person[] per; public Person(Person[] array)//重载构造函数,迭代对象
{
per = new Person[array.Length];//创建对象
for (int i = ; i < array.Length; i++)//遍历初始化对象
{
per[i] = array[i];//数组赋值
}
} public IEnumerator GetEnumerator()//实现接口
{
return new PersonEnum(per);
}
} class PersonEnum : IEnumerator//实现foreach语句内部,并派生
{
public Person[] _per;//实现数组
int position = -;//设置“指针” public PersonEnum(Person[] list)
{
_per = list;//实现list
} public bool MoveNext()//实现向前移动
{
position++;//位置增加
return (position < _per.Length);//返回布尔值
} public void Reset()//位置重置
{
position = -;//重置指针为-1
} public object Current//实现接口方法
{
get
{
try
{
return _per[position];//返回对象
}
catch (IndexOutOfRangeException)//捕获异常
{
throw new InvalidOperationException();//抛出异常信息
}
}
}
} class Program
{
static void Main(string[] args)
{
Person[] per = new Person[]
{
new Person("Jack",""),
new Person("David",""),
};
Person personlist = new Person(per);
//遍历对象
foreach (Person p in personlist)
{
Console.WriteLine("Name is " + p.Name + " and Age is " + p.Age);
}
Console.ReadKey();
}
}
}
原文链接:http://www.studyofnet.com/news/452.html
相关博客1:http://blog.csdn.net/callmeback/article/details/8295648
相关博客2:http://www.cnblogs.com/shaosks/archive/2011/09/27/2193270.html
IEnumerable、IEnumerator、ICollection、IList、List的继承关系及简单使用的更多相关文章
- IEnumerable、ICollection、IList、List之间的区别与方法介绍
区别 以下列出IEnumerable.ICollection.IList.List继承关系.(这里带有泛型,非泛型也是一样的关系) IEnumerable<T>: public inter ...
- C#中IEnumerable、ICollection、IList、List之间的区别
IEnumerable.ICollection.IList.List之间的区别,本文分别分析了它的实现源码,从而总结出了它们之间的关系和不同之处. 首先我看看 IEnumerable: // 摘要: ...
- 【转载】我也说 IEnumerable,ICollection,IList,List之间的区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- Asp.Net IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- IEnumerable,ICollection,IList,List区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: / ...
- IEnumerable,ICollection,IList,List之间的区别
做C#的同学们,都知道,一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 // 摘要: // 公开枚举器,该枚举器 ...
- 服务器文档下载zip格式 SQL Server SQL分页查询 C#过滤html标签 EF 延时加载与死锁 在JS方法中返回多个值的三种方法(转载) IEnumerable,ICollection,IList接口问题 不吹不擂,你想要的Python面试都在这里了【315+道题】 基于mvc三层架构和ajax技术实现最简单的文件上传 事件管理
服务器文档下载zip格式 刚好这次项目中遇到了这个东西,就来弄一下,挺简单的,但是前台调用的时候弄错了,浪费了大半天的时间,本人也是菜鸟一枚.开始吧.(MVC的) @using Rattan.Co ...
- IEnumerable,ICollection,IList,List的使用
做C#的都知道:一类只能有一个继承类,但可以实现多个接口.这句话就告诉我们:IEnumerable,ICollection,IList,List区别了 首先我看看 IEnumerable: // ...
- IEnumerable<> ICollection <> IList<> 区别
IEnumerable< ICollection < IList区别 public interface IEnumerable { IEnumerator GetEnumerator(); ...
随机推荐
- linux 下nginx 集群CAS单点登录实现
1.单点登录服务器CAS应用配置于tomcat下. 1)key生成: keytool -genkey -alias mycas -keyalg RSA -keysize 2048 -keystore ...
- Spring Cloud 2-Feign 声明式服务调用(三)
Spring Cloud Feign 1. pom.xml 2. application.yml 3. Application.java 4. Client.java 简化RestTemplate调 ...
- WPF 窗口去除顶部边框(正宗无边框)
最近在做一个大屏展示视频图片的项目,功能并不复杂,半天的工作量吧,一开始同事采用的Unity3D进行开发,但是里面要播放4K视频,Unity 的短板就是视频的播放了,今晚就要交付了,我一早就来公司,决 ...
- 027_git添加多账号设置
一. 注意事项: (1)公钥文件权限设置问题 现象: Permissions 0644 for '/Users/arunyang/.ssh/id_rsa_ele_me.pub' are too ope ...
- 【原创】大数据基础之Zookeeper(3)选举算法
提到zookeeper选举算法,就不得不提Paxos算法,因为zookeeper选举算法是Paxos算法的一个变种: Paxos要解决的问题是:在一个分布式网络环境中有众多的参与者,但是每个参与者都不 ...
- [转]Ubuntu中apt与apt-get命令的区别
转载于https://www.sysgeek.cn/apt-vs-apt-get/ Ubuntu 16.04 发布时,一个引人注目的新特性便是 apt 命令的引入.其实早在 2014 年,apt 命令 ...
- svn"database disk image is malformed"错误解决
本文是svn出现类似如下问题的两种解决方案. svn: E200030: database disk image is malformed 一.最简单的方法,复制其它人的.svn/wc.db替换. 二 ...
- CSS之三个模型 盒子模型 轮廓模型 内外边距
盒子模型 最终元素的总宽度计算公式是这样的: 总元素的宽度=宽度+左填充+右填充+左边框+右边框+左边距+右边距 元素的总高度最终计算公式是这样的: 总元素的高度=高度+顶部填充+底部填充+上边框+下 ...
- js 获取两个时间戳之间相隔多少天多少小时多少分多少秒
<script> function getRemainderTime (startTime){ var s1 = new Date(startTime.replace(/-/g, &quo ...
- ios 修改导航栏返回按钮的图片
修改导航栏返回按钮的图片 方法1: [UINavigationBar appearance].backIndicatorTransitionMaskImage = [UIImage imageName ...