1.ArrayList ArrayList list = new ArrayList(); //for遍歷 for (int i = 0; i < list.Count; i++) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } //foreac
1.ArrayList
ArrayList list = new ArrayList(); //for遍歷 for (int i = 0; i < list.Count; i++) { SE se=(SE)list[i]; Console.WriteLine(se.Name); } //foreach遍歷 foreach(Object obj in list) { SE se=(SE)list[i]; Console.WriteLine(se.Name); }
2.Hashtable
Hashtable list=new Hashtable(); //Hashtable不能用索引訪問,所以遍歷一個Hashtable只能用foreache()方法 //遍歷Key foreach(Object obj in list.Keys) { Console.WriteLine((string)obj); } //遍歷Value foreach(Object obj in list.Values) { SE se=(SE)obj; Console.WriteLine(se.Name); } //同時遍歷,DictionaryEntry一個結構 foreach(DictionaryEntry en in list){ Console.WriteLine(se.Keys); COnsole.WriteLine(((SE)en.Value).Name); }
3.List<T>
//for遍歷 for (int i = 0; i < list.Count; i++) { //遍歷時不需要類型轉換 Console.WriteLine(list[i]); } //foreach遍歷 foreach(SE obj in list) { //遍歷時不需要類型轉換 Console.WriteLine(obj); } //
4.Dictionary<K,V>
//遍歷Values foreach(SE se in list.Values) { //遍歷時不需要類型轉換 Console.WriteLine(se); } //同時遍歷 foreach(KeyValuePair<string,SE> en in list) { Console.WriteLine(en.Key); Console.WriteLine(en.Value.Name); } //KeyValuePair<TKey,TValue>是一個泛型結構