温故而知新,可以为师矣,有空经常复习一下基础知识是有必要的,并且能加深理解和记忆。
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); } } |
结果显示:
果然不出所料,真正做事情的是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(); } |
结果显示:
大功告成,再扩展成通用的容器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); } |
结果显示:
ienumerator迭代器接口挺啰嗦的,yield是简化了遍历的语法糖而已。
以上就是本文的全部内容,希望本文的内容对大家的学习或者工作能带来一定的帮助,同时也希望多多支持服务器之家!
原文链接:http://www.cnblogs.com/EminemJK/p/6428282.html