using System; //引用System命名空间
public class MyCollection //创建一个类 MyCollection { int[] items;
public MyCollection() //这条语句是继承 MyCollection 类的么? { items = new int[5] {12, 44, 33, 2, 50}; 为什么不写成int items=new int[5]{12,44,33,2,50}呢?是整形变量的关键字可以省略的么? }
public MyEnumerator GetEnumerator() { return new MyEnumerator(this); //return在这里具体的意思是什么?请指教,谢谢!!! } public class MyEnumerator { int nIndex; MyCollection collection; //是类型的继承么? public MyEnumerator(MyCollection coll) //此类有什么意思,能解释下么?谢谢!!! { collection = coll; nIndex = -1; }
public bool MoveNext() //请解释,谢谢!!! { nIndex++; return(nIndex < collection.items.GetLength(0)); }
public int Current //请解释,谢谢!!! { get { return(collection.items[nIndex]); } } } }
public class MainClass { public static void Main() { MyCollection col = new MyCollection(); Console.WriteLine("Values in the collection are:");
// Display collection items: foreach (int i in col) { Console.WriteLine(i); } } }