对象数组,使用foreach循环.必须要继承IEnumerable接口.实现GetEnumerator()方法.
下面是一个测试用的例子.
1using System;
2using System.Collections;
3
4public class MyCollection : IEnumerable
5{
6 protected ArrayList collection = new ArrayList(1024);
7
8 public MyCollection()
9 {
10 }
11
12 public object this[int index]
13 {
14 get { return collection[index]; }
15 }
16
17 public int Count
18 {
19 get { return collection.Count; }
20 }
21
22 internal virtual int Add(object objMember)
23 {
24 return collection.Add(objMember);
25 }
26
27
28 public IEnumerator GetEnumerator()
29 {
30 return collection.GetEnumerator();
31 }
32}
33
34public class MainClass
35{
36 public static void Main(string [] args)
37 {
38 MyCollection myCollection = new MyCollection();
39 myCollection.Add("111");
40 myCollection.Add("222");
41 myCollection.Add("333");
42 myCollection.Add("444");
43 myCollection.Add("555");
44
45 foreach (object i in myCollection)
46 {
47 Console.WriteLine(i.ToString());
48 }
49 Console.ReadLine();
50 }
51}
52
2using System.Collections;
3
4public class MyCollection : IEnumerable
5{
6 protected ArrayList collection = new ArrayList(1024);
7
8 public MyCollection()
9 {
10 }
11
12 public object this[int index]
13 {
14 get { return collection[index]; }
15 }
16
17 public int Count
18 {
19 get { return collection.Count; }
20 }
21
22 internal virtual int Add(object objMember)
23 {
24 return collection.Add(objMember);
25 }
26
27
28 public IEnumerator GetEnumerator()
29 {
30 return collection.GetEnumerator();
31 }
32}
33
34public class MainClass
35{
36 public static void Main(string [] args)
37 {
38 MyCollection myCollection = new MyCollection();
39 myCollection.Add("111");
40 myCollection.Add("222");
41 myCollection.Add("333");
42 myCollection.Add("444");
43 myCollection.Add("555");
44
45 foreach (object i in myCollection)
46 {
47 Console.WriteLine(i.ToString());
48 }
49 Console.ReadLine();
50 }
51}
52