# C#迭代器

185人阅读 评论(0)

1.foreach的运行机制
2.传统集合的遍历
3.使用迭代器

### 一、foreach的运行机制

 1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5
6 namespace IteratorDemo
7 {
8     class Person
9     {
10         public Person(string fName, string lName)
11         {
12             this.firstName = fName;
13             this.lastName = lName;
14         }
15
16         public string firstName;
17         public string lastName;
18     }
19 }

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person[] persons = new Person[] {
14                 new Person("Kenshin","Cui"),
15                 new Person("Miaoer","Sun"),
16                 new Person("Jinjuan","Shen"),
17                 new Person("Yanxin","Nie")
18             };
19             foreach (Person p in persons)
20             {
21                 Console.WriteLine(p.firstName + " " + p.lastName);
22             }
24         }
25
26     }
27 }

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class Program
10     {
11         static void Main(string[] args)
12         {
13             Person[] persons = new Person[] {
14                 new Person("Kenshin","Cui"),
15                 new Person("Miaoer","Sun"),
16                 new Person("Jinjuan","Shen"),
17                 new Person("Yanxin","Nie")
18             };
19
20             //等价的方法
21             IEnumerator enuerator = persons.GetEnumerator();
22             while (enuerator.MoveNext())
23             {
24                 Person p = enuerator.Current as Person;
25                 Console.WriteLine(p.firstName + " " + p.lastName);
26             }
28         }
29
30     }
31 }

### 二、传统集合的遍历

People类
 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class People:IEnumerable
10     {
11         private Person[] _people;
12         public People(Person[] pArray)
13         {
14             _people = new Person[pArray.Length];
15
16             for (int i = 0; i < pArray.Length; i++)
17             {
18                 _people[i] = pArray[i];
19             }
20         }
21
22         public IEnumerator GetEnumerator()
23         {
24             return new PeopleEnum(_people);
25         }
26     }
27 }
PeopleEnum类
 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class PeopleEnum:IEnumerator
10     {
11         public Person[] _people;
12         int position = -1;
13
14         public PeopleEnum(Person[] list)
15         {
16             _people = list;
17         }
18
19         public bool MoveNext()
20         {
21             position++;
22             return (position < _people.Length);
23         }
24
25         public void Reset()
26         {
27             position = -1;
28         }
29
30         public object Current
31         {
32             get
33             {
34                 try
35                 {
36                     return _people[position];
37                 }
38                 catch (IndexOutOfRangeException)
39                 {
40                     throw new InvalidOperationException();
41                 }
42             }
43         }
44     }
45 }

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class Program
10     {
11         /*
12          * 实现IEnumerable（显然目前我们必须先实现IEnumerator）
13          */
14         static void Main(string[] args)
15         {
16             Person[] persons = new Person[] {
17                 new Person("Kenshin","Cui"),
18                 new Person("Miaoer","Sun"),
19                 new Person("Jinjuan","Shen"),
20                 new Person("Yanxin","Nie")
21             };
22             People pe = new People(persons);
23             foreach (Person p in pe)
24             {
25                 Console.WriteLine(p.firstName + " " + p.lastName);
26             }
28         }
29
30     }
31 }

### 三、使用迭代器

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class People:IEnumerable
10     {
11         private Person[] _people;
12         public People(Person[] pArray)
13         {
14             _people = new Person[pArray.Length];
15
16             for (int i = 0; i < pArray.Length; i++)
17             {
18                 _people[i] = pArray[i];
19             }
20         }
21
22         public IEnumerator GetEnumerator()
23         {
24             for (int i = 0; i < _people.Length; ++i)
25             {
26                 yield return _people[i];
27             }
28         }
29     }
30 }

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class Program
10     {
11         private static IList<Person> FilterPeople(IEnumerable<Person> people)
12         {
13             IList<Person> persons = new List<Person>();
14             foreach (Person p in people)
15             {
16                 if (p.lastName == "Cui")
17                 {
19                 }
20             }
21             return persons;
22         }
23         static void Main(string[] args)
24         {
25             Person[] persons = new Person[] {
26                     new Person("Kenshin","Cui"),
27                     new Person("Miaoer","Sun"),
28                     new Person("Jinjuan","Shen"),
29                     new Person("Yanxin","Nie")
30             };
31             foreach(Person p in FilterPeople(persons))
32             {
33                 Console.WriteLine(p.firstName + " " + p.lastName);
34             }
36         }
37     }
38 }

 1 using System;
2 using System.Collections;
3 using System.Collections.Generic;
4 using System.Linq;
5 using System.Text;
6
7 namespace IteratorDemo
8 {
9     class Program
10     {
11         /*
12          * 使用迭代快简化类似的方法
13          */
14         private static IEnumerable<Person> FilterPeople(IEnumerable<Person> people)
15         {
16             foreach(Person p in people)
17             {
18                 if(p.lastName=="Cui")
19                 {
20                     yield return p;
21                 }
22             }
23         }
24         static void Main(string[] args)
25         {
26             Person[] persons = new Person[] {
27                     new Person("Kenshin","Cui"),
28                     new Person("Miaoer","Sun"),
29                     new Person("Jinjuan","Shen"),
30                     new Person("Yanxin","Nie")
31             };
32             foreach(Person p in FilterPeople(persons))
33             {
34                 Console.WriteLine(p.firstName + " " + p.lastName);
35             }
37         }
38     }
39 }

0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：825128次
• 积分：13221
• 等级：
• 排名：第992名
• 原创：435篇
• 转载：480篇
• 译文：1篇
• 评论：85条
阅读排行
最新评论