码迷,mamicode.com
首页 > 其他好文 > 详细

foreach循环

时间:2017-10-28 16:30:26      阅读:122      评论:0      收藏:0      [点我收藏+]

标签:对象   pac   []   class   写入   第一条   col   task   threading   

进行foreach遍历的时候,实际上调用的是GetEnumerator这个方法,反编译的IL语言中是没有foreach的。当我们想要对实例化的对象进行foreach遍历的时候,会报错。那么我们就手动写一个foreach-枚举器

 1 using System;
 2 using System.Collections;
 3 using System.Collections.Generic;
 4 using System.Linq;
 5 using System.Text;
 6 using System.Threading.Tasks;
 7 
 8 namespace foreach_01
 9 {
10     class Program
11     {
12         static void Main(string[] args)
13         {
14             Person p = new Person();
15             foreach (string item in p)
16             {
17                 Console.WriteLine(item);
18             }
19             Console.WriteLine("OK");
20             Console.ReadKey();
21         }
22     }
23     //1.需要让该类型实现一个名字叫IEnumerable的接口,实现该接口的主要目的是为了让当前类型中增加一个名字是GetEnumeerrator()的方法
24 
25     public class Person:IEnumerable
26     {
27         private string[] Friends = new string[] { "周氏","刘晓","邓丽君","孙权"};
28          
29         public string Name
30         {
31             get;
32             set;
33         }
34         public int Age
35         {
36             get;
37             set;
38         }
39         public string Email
40         {
41             get;
42             set;
43         }
44         //需要返回一个枚举器
45         public IEnumerator GetEnumerator()
46         {
47             //1.在这个方法中写什么代码?
48             return new PersonEnumerator(this.Friends);
49         }
50     }
51     //这个类型就是一个枚举器
52     //希望一个类型被枚举,遍历,就要实现一个类,该类就是枚举器
53     public class PersonEnumerator : IEnumerator
54     {
55         public PersonEnumerator(string[] fs)
56         {
57             _friends = fs;
58         }
59         private string [] _friends;
60         //下标一般都是一开始指向了第一条的前一条
61         private int index = -1;
62 
63         public object Current
64         {
65             get 
66             {
67                 if (index >= 0 && index < _friends.Length)
68                 {
69                     return _friends[index];
70                 }
71                 else
72                 {
73                     throw new IndexOutOfRangeException();
74                 }
75             }
76         }
77 
78         public bool MoveNext()
79         {
80             if (index + 1 < _friends.Length)
81             {
82                 index++;
83                 return true;
84             }
85             return false;
86         }
87 
88         public void Reset()
89         {
90             index = -1;
91         }
92     }
93 }

直接写一个GetEnumerator方法也是可以实现这个枚举器的,而使用接口实现也是为了实现多态。

当在实例类中写入下面的代码时,编译器会给你自动生成一个枚举器

public IEnumerator GetNumerator()
{
for (int i = 0; i < Friends.Length; i++)
{
yield return Friends[i];
}
}

foreach循环

标签:对象   pac   []   class   写入   第一条   col   task   threading   

原文地址:http://www.cnblogs.com/nihaoshui/p/7746970.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!