collection modification problems

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • csharpula csharp

    #1

    collection modification problems

    Hello,

    I am trying to iterate through list:

    foreach (classA objectA in listA)
    {
    //if there is no objectA with specific name in list A then:

    listA.add(objec tA)

    }

    But it seems like this is an exception to modify the collection that I
    am iterationg through. How to solve this?

    Thank you!


    *** Sent via Developersdex http://www.developersdex.com ***
  • Jeroen Mostert

    #2
    Re: collection modification problems

    csharpula csharp wrote:
    I am trying to iterate through list:
    >
    foreach (classA objectA in listA)
    {
    //if there is no objectA with specific name in list A then:
    >
    listA.add(objec tA)
    >
    }
    >
    But it seems like this is an exception to modify the collection that I
    am iterationg through. How to solve this?
    >
    One option is to, well, not modify the collection you're iterating through,
    but use a copy instead:

    foreach (classA objectA in new List<classA>(li stA)) { // or listA.ToArray()
    ...
    listA.Add(objec tA);
    }

    Another option is to iterate in a way that will not fail when new elements
    are added:

    int count = listA.Count;
    for (int i = 0; i != count; ++i) {
    classA objectA = listA[i];
    ...
    listA.Add(objec tA);
    }

    Finally, you could produce the results as a new list and combine them
    afterwards:

    List<classAlist B = new List<classA>();
    foreach (classA objectA in listA) {
    ...
    listB.Add(objec tA);
    }
    listA.AddRange( listB);

    If all you're actually doing is appending elements, option #2 will generally
    be the most efficient since no additional memory is allocated beyond what
    you need for the extra elements.

    --
    J.

    Comment

    • PvdG42

      #3
      Re: collection modification problems

      "csharpula csharp" <csharpula@yaho o.comwrote in message
      news:OD3cGY3NJH A.588@TK2MSFTNG P06.phx.gbl...
      Hello,
      >
      I am trying to iterate through list:
      >
      foreach (classA objectA in listA)
      {
      //if there is no objectA with specific name in list A then:
      >
      listA.add(objec tA)
      >
      }
      >
      But it seems like this is an exception to modify the collection that I
      am iterationg through. How to solve this?
      >
      Thank you!
      >
      >
      *** Sent via Developersdex http://www.developersdex.com ***

      I hope I understand you correctly.
      You want to add an item to a collection if it is not already there?
      Depending on the size of your list and the frequency of the search, you
      could be adding unnecessary overhead to your application.
      Have you considered using a collection type that does not allow duplicates?

      http://msdn.microsoft.com/en-us/libr...s.generic.aspx

      Comment

      Working...