|
|
| | |
Collections Posted: 27 May 09 10:39 AM (United States) |
| | |
1) Deleting an item from a collection inside foreach loop If you delete an item inside a foreach loop, you will get an exception. to workaround this problem, you can copy it into another arrayList. another solution is just iterate backwards through the list
private static void testArrayList() { ArrayList myArrayList = new ArrayList(); for (int i=0; i< 10; i++) { myArrayList.Add("test" + i.ToString()); } for (int i= myArrayList.Count - 1; i>= 0; i--) { string s= (string)myArrayList[i]; if (s== "test5") { myArrayList.RemoveAt(i); } } }
2) here is a non type-safe generic collection
List private static void testList() { List |
| |
| | .NET Development MCTS 70-536 Study Group Collections | | |
| | | |