A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 刘安成 中级黑马   /  2013-12-23 01:17  /  1586 人查看  /  6 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 刘安成 于 2013-12-24 01:24 编辑

用foreach遍历自定义集合类?

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

6 个回复

倒序浏览
实现IEnumerable接口,GetIEnumerable方法返回一个实现IEnumrator接口的枚举数,或者使用yield. return 迭代器实现都行。

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

回复 使用道具 举报
师弟 发表于 2013-12-23 01:47
实现IEnumerable接口,GetIEnumerable方法返回一个实现IEnumrator接口的枚举数,或者使用yield. return 迭 ...

:):):):):):):):):):):):):):):):):):):):)
回复 使用道具 举报
就拿string与char类型来讲。其实string中的字符串类型大家都可看把它看做成是char的数组 那么既然string 是char 的数据那么就能通过遍历来进行读取。 1...一个是用for循环进行遍历 2...一个是用foreach 进行遍历 eg:    string str="advdsa";    char cc='';  foreach(char c in str)       cc=c;
回复 使用道具 举报
使用foreach遍历,该对象类必须有 GetEnumerator、MoveNet、Reset 和 Current 成员。有两种方法可有实现,一种是实现枚举器接口(IEnumerator)和可枚举接口(IEnumerable),另一种是使用 yield return 迭代语句,这种方法有3种迭代器。
详细可有参考:http://msdn.microsoft.com/zh-cn/library/dscyy5s0(v=vs.100).aspx

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

回复 使用道具 举报
IEnumerable 接口



公开枚举数,该枚举数支持在非泛型集合上进行简单迭代。

IEnumberable接口只有一个方法:

    GetEnumerator  返回一个循环访问集合的枚举数。

    GetEnumerator  C#语法:

            IEnumerator GetEnumerator()





示例:

下面的代码示例演示如何实现自定义集合的 IEnumerable 和 IEnumerator 接口。在此示例中,没有显式调用这些接口的成员,但实现了它们,以便支持使用 foreach(在 Visual Basic 中为 For Each)循环访问该集合。




[c-sharp] view plaincopy
01.using System;  
02.using System.Collections;  
03.  
04.public class Person  
05.{  
06.    public Person(string fName, string lName)  
07.    {  
08.        this.firstName = fName;  
09.        this.lastName = lName;  
10.    }  
11.  
12.    public string firstName;  
13.    public string lastName;  
14.}  
15.  
16.public class People : IEnumerable  
17.{  
18.    private Person[] _people;  
19.    public People(Person[] pArray)  
20.    {  
21.        _people = new Person[pArray.Length];  
22.  
23.        for (int i = 0; i < pArray.Length; i++)  
24.        {  
25.            _people[i] = pArray[i];  
26.        }  
27.    }  
28.  
29.    public IEnumerator GetEnumerator()  
30.    {  
31.        return new PeopleEnum(_people);  
32.    }  
33.}  
34.  
35.public class PeopleEnum : IEnumerator  
36.{  
37.    public Person[] _people;  
38.  
39.    // Enumerators are positioned before the first element  
40.    // until the first MoveNext() call.  
41.    int position = -1;  
42.  
43.    public PeopleEnum(Person[] list)  
44.    {  
45.        _people = list;  
46.    }  
47.  
48.    public bool MoveNext()  
49.    {  
50.        position++;  
51.        return (position < _people.Length);  
52.    }  
53.  
54.    public void Reset()  
55.    {  
56.        position = -1;  
57.    }  
58.  
59.    public object Current  
60.    {  
61.        get  
62.        {  
63.            try  
64.            {  
65.                return _people[position];  
66.            }  
67.            catch (IndexOutOfRangeException)  
68.            {  
69.                throw new InvalidOperationException();  
70.            }  
71.        }  
72.    }  
73.}  
74.  
75.class App  
76.{  
77.    static void Main()  
78.    {  
79.        Person[] peopleArray = new Person[3]  
80.        {  
81.            new Person("John", "Smith"),  
82.            new Person("Jim", "Johnson"),  
83.            new Person("Sue", "Rabon"),  
84.        };  
85.  
86.        People peopleList = new People(peopleArray);  
87.        foreach (Person p in peopleList)  
88.            Console.WriteLine(p.firstName + " " + p.lastName);  
89.  
90.    }  
91.}  
92.  
93./* This code produces output similar to the following:
94. *  
95. * John Smith
96. * Jim Johnson
97. * Sue Rabon
98. *  
99. */  

评分

参与人数 1技术分 +1 收起 理由
茹化肖 + 1

查看全部评分

回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马