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(); ...
随机推荐
- L1-Day2
L1-Day21.明智的人从来不生气. [我的翻译]A wise man is never angry. [标准答案]A wise man never gets angry. [对比分析]be和get ...
- MVC RedirectToAction 跳转时传参问题
RedirectToAction方法提供了5个重载方法 1.单纯跳转,不带参数. string redirectUrl = "/List" ; return RedirectToA ...
- 其他-pkuwc2019数学考试题目
时限150min,有windows和Ubuntu使用 十道填空题,在poj上举行,选手提交答案,系统将答案自动填入一个作用是输出答案的程序,再将该程序提交评测(由于该程序变量名为longlong,所以 ...
- Windows caffe 跑mnist实例
一. 装完caffe当然要来跑跑自带的demo,在examples文件夹下. 先来试试用于手写数字识别的mnist,在 examples/mnist/ 下有需要的代码文件,但是没有图像库. mn ...
- mybatis 查询单个对象,结果集类型一定要明确
简单介绍:用ssm框架已经有很长时间了,但是似乎从来都没有对于查询单个对象,存在问题的,好像也就是那回事,写完sql就查出来了,也从来都没有认真的想过,为什么会这样,为什么要设置结果集类型 代码: / ...
- Sharding-JDBC
1.官网文档:https://shardingsphere.apache.org/document/legacy/3.x/document/cn/manual/sharding-jdbc/usage/ ...
- Apache服务器中设置端口映射和反向代理的方法
在/etc/httpd/conf路径下的httpd.conf文件###new add for webui.cong###Include "E:/local/Wamp/bin/apache/A ...
- 连接慢的主要原因是DNS解析导致
连接慢的主要原因是DNS解析导致解决方法: 1.在ssh服务端上更改/etc/ssh/sshd_config文件中的配置为如下内容:UseDNS no# GSSAPI optionsGSSAPIAut ...
- Knockout中ko.utils中处理数组的方法集合
每一套框架基本上都会有一个工具类,如:Vue中的Vue.util.Knockout中的ko.utils.jQuery直接将一些工具类放到了$里面,如果你还需要更多的工具类可以试试lodash.本文只介 ...
- python之基于libsvm识别数字验证码
1. 参考 字符型图片验证码识别完整过程及Python实现 2.图片预处理和手动分类 (1)分析图片 from PIL import Image img = Image.open('nums/ttt. ...