服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - C# - C# IEnumerable和IEnumerator接口浅析

C# IEnumerable和IEnumerator接口浅析

2021-12-27 14:45山治先生 C#

本文主要介绍了C#中IEnumerable和IEnumerator接口的相关知识,具有很好的参考价值,下面跟着小编一起来看下吧

温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆。

foreach常用于循环访问集合,对实现ienumerable的接口的容器进行遍历,ienumerable和ienumerator接口我有时候也有点迷糊,按官方的解释,ienumerable是枚举器接口,ienumerator是迭代器接口,从字面意思来看相差不大,逐一分析一下。

ienumerable接口

?
1
2
3
4
public interface ienumerable
{
 ienumerator getenumerator();
}

继承ienumerable接口的类需实现暴露出来的getenumerator()方法,并返回一个ienumerator接口对象,看来真正做事的是ienumerator,f12看一下ienumerator又有什么鬼东西。

ienumerator接口

?
1
2
3
4
5
6
public interface ienumerator
{
 object current { get; }
 bool movenext();
 void reset();
}

ienumerator接口有三个东东,一个属性current,返回当前集合中的元素,方法movenext()移动到下一个,遍历不都是向后遍历的嘛,reset(),字面意思重置,这个容易理解。做个假设:既然ienumerable接口返回是ienumerator接口迭代器来实现的,那么仅继承ienumerator迭代器接口能不能实现一个自定义容器?

定义一个phone类

?
1
2
3
4
5
6
7
8
public class phone
{
 public string name;
 public phone(string name)
 {
  this.name = name;
 }
}

定义一个名为myenumerator迭代器,并现实它接口ienumerator

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
public class myenumerator : ienumerator
{
 phone[] p;
 int idx = -1;
 public myenumerator(phone[] t)
 {
  p = t;
 }
 public object current
 {
  get
  {
  if (idx == -1)
   return new indexoutofrangeexception();
  return p[idx];
  }
 }
 public bool movenext()
 {
  idx++;
  return p.length > idx;
 }
 public void reset()
 {
  idx = -1;
 }
}
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
class program
 {
 static void main(string[] args)
 {
       show("-----------ienumerator------------");
  phone[] phones = new phone[] { new phone("iphone 7s"), new phone("iphone 6s"), new phone("iphone 5s") };
  myenumerator enumerator = new myenumerator(phones);
  while (enumerator.movenext())
  {
  phone p = enumerator.current as phone;
  show(p.name);
  }
  console.readkey();
 }
 static void show(string i)
 {
  console.writeline(i);
 }
 }

结果显示:

C# IEnumerable和IEnumerator接口浅析

果然不出所料,真正做事情的是ienumerator接口,即可循环访问自定义的一个容器,不过,初衷是想用foreach来做循环访问、遍历的。那好,那就只能显示ienumerable接口来做。稍稍改造一下phone类:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
public class phone : ienumerable
 {
 public string name ;
 public phone(string name)
 {
  this.name = name;
 }
 phone[] p;
 public phone(phone[] t)
 {
  p = t;
 }
 public ienumerator getenumerator()
 {
  return new myenumerator(p);
 }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
static void main(string[] args)
 {
  show("-----------ienumerator------------");
  phone[] phones = new phone[] { new phone("iphone 7s"), new phone("iphone 6s"), new phone("iphone 5s") };
  myenumerator enumerator = new myenumerator(phones);
  while (enumerator.movenext())
  {
  phone p = enumerator.current as phone;
  show(p.name);
  }
  show("-----------ienumerable------------");
  phone phonelist = new phone(phones);
  foreach (phone p in phonelist)
  {
  show(p.name);
  }
  console.readkey();
 }

结果显示:

C# IEnumerable和IEnumerator接口浅析

大功告成,再扩展成通用的容器phonepackage,继承泛型ienumerable<t>接口即可。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class phonepackage<t> : ienumerable<t>
 {
 private list<t> datalist = null;
 public void add(t t)
 {
  if (datalist == null)
  datalist = new list<t>();
  datalist.add(t);
 }
 public ienumerator<t> getenumerator()
 {
  foreach (t t in datalist)
  {
  yield return t;
  }
 }
 ienumerator ienumerable.getenumerator()
 {
  foreach (t t in datalist)
  {
  yield return t;
  }
 }
 }
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
static void main(string[] args)
 {
  show("-----------ienumerator------------");
  phone[] phones = new phone[] { new phone("iphone 7s"), new phone("iphone 6s"), new phone("iphone 5s") };
  myenumerator enumerator = new myenumerator(phones);
  while (enumerator.movenext())
  {
  phone p = enumerator.current as phone;
  show(p.name);
  }
  show("-----------ienumerable------------");
  phone phonelist = new phone(phones);
  foreach (phone p in phonelist)
  {
  show(p.name);
  }
  show("-----------ienumerable<t>------------");
  phonepackage<phone> phonepackage = new phonepackage<phone>();
  phonepackage.add(new phone("iphone 7s"));
  phonepackage.add(new phone("iphone 6s"));
  phonepackage.add(new phone("iphone 5s"));
  foreach (phone p in phonepackage)
  {
  show(p.name);
  }
  console.readkey();
 }
 static void show(string i)
 {
  console.writeline(i);
 }

结果显示:

C# IEnumerable和IEnumerator接口浅析

ienumerator迭代器接口挺啰嗦的,yield是简化了遍历的语法糖而已。

以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!

原文链接:http://www.cnblogs.com/EminemJK/p/6428282.html

延伸 · 阅读

精彩推荐
  • C#浅谈C# winForm 窗体闪烁的问题

    浅谈C# winForm 窗体闪烁的问题

    下面小编就为大家带来一篇浅谈C# winForm 窗体闪烁的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧...

    C#教程网7962021-12-21
  • C#聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题 新手速来围观

    聊一聊C#接口问题,新手速来围观,一个通俗易懂的例子帮助大家更好的理解C#接口问题,感兴趣的小伙伴们可以参考一下...

    zenkey7072021-12-03
  • C#C# 后台处理图片的几种方法

    C# 后台处理图片的几种方法

    本篇文章主要介绍了C# 后台处理图片的几种方法,非常具有实用价值,需要的朋友可以参考下。...

    IT小伙儿10162021-12-08
  • C#Unity3D UGUI实现缩放循环拖动卡牌展示效果

    Unity3D UGUI实现缩放循环拖动卡牌展示效果

    这篇文章主要为大家详细介绍了Unity3D UGUI实现缩放循环拖动展示卡牌效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参...

    诗远3662022-03-11
  • C#C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    C#实现的文件操作封装类完整实例【删除,移动,复制,重命名】

    这篇文章主要介绍了C#实现的文件操作封装类,结合完整实例形式分析了C#封装文件的删除,移动,复制,重命名等操作相关实现技巧,需要的朋友可以参考下...

    Rising_Sun3892021-12-28
  • C#c#学习之30分钟学会XAML

    c#学习之30分钟学会XAML

    一个界面程序的核心,无疑就是界面和后台代码,而xaml就是微软为构建应用程序界面而创建的一种描述性语言,也就是说,这东西是搞界面的...

    C#教程网8812021-12-10
  • C#C#基础之泛型

    C#基础之泛型

    泛型是 2.0 版 C# 语言和公共语言运行库 (CLR) 中的一个新功能。接下来通过本文给大家介绍c#基础之泛型,感兴趣的朋友一起学习吧...

    方小白7732021-12-03
  • C#C#直线的最小二乘法线性回归运算实例

    C#直线的最小二乘法线性回归运算实例

    这篇文章主要介绍了C#直线的最小二乘法线性回归运算方法,实例分析了给定一组点,用最小二乘法进行线性回归运算的实现技巧,具有一定参考借鉴价值,需要...

    北风其凉8912021-10-18