Robert Half Technology
 Search
Friday, July 30, 2010 ..:: Community Forum ::.. Register  Login
 Community Forum Minimize
     
  .NET Development  MCTS 70-536 Study Group  Collections  

 Collections 
 
Khayralla
12 posts
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 list = new List();
list.Add(10);
list.Add("test");
list.Add(true);
int x = (int) list[0];
string text = (string)
list[1]; bool value = (bool)list[2];
}

I hope this will help somebody.
 
   .NET Development  MCTS 70-536 Study Group  Collections
 
 
   

Search   

Copyright 2008 Ottawa .NET Community   Terms Of Use  Privacy Statement
Portal engine source code is copyright 2002-2010 by DotNetNuke. All Rights Reserved