static void Main(string[] args) { Console.WriteLine("*********Fun with IEnumberable/IEnumerator************\n"); Garage carLot = new Garage(); //交出集合中的每一Car对象吗 foreach (Car c in carLot) //之所以遍历carLot,是因为carLot.GetEnumerator()返回的项时Car类型,这个十分重要 { Console.WriteLine("{0} is going {1} MPH", c.CarName, c.CurrentSpeed); } Console.WriteLine("GetEnumerator被定义为公开的,对象用户可以与IEnumerator类型交互,下面的结果与上面是一致的"); //手动与IEnumerator协作 IEnumerator i = carLot.GetEnumerator(); while (i.MoveNext()) { Car myCar = (Car)i.Current; Console.WriteLine("{0} is going {1} MPH", myCar.CarName, myCar.CurrentSpeed); } Console.WriteLine("回车继续"); Console.ReadLine(); // ForeachTest f = new ForeachTest("This is a sample sentence.", new char[] { ' ', '-' }); string[] s = new string[] { "This", "is", "a", "sample", "sentence." }; ForeachTest f = new ForeachTest("This,is,a,sample,sentence."); foreach (string item in f) { System.Console.WriteLine(item); } Console.WriteLine("回车继续"); Console.ReadLine(); //创建一个新的列表框并初始化 ListBoxTest lbt = new ListBoxTest("Hello", "World"); //添加新的字符串 lbt.Add("Who"); lbt.Add("Is"); lbt.Add("Douglas"); lbt.Add("Adams"); //测试访问 string subst = "Universe"; lbt[1] = subst; //访问所有的字符串 foreach (string ss in lbt) { Console.WriteLine("Value:{0}", ss); } Console.ReadLine(); }
public ForeachTestEnumerator(ForeachTest t) { this.t = t; }