Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Removing items from list while performing operations on it

Removing items from list while performing operations on it

Scheduled Pinned Locked Moved C#
questiondatabasehelptutoriallounge
3 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • X Offline
    X Offline
    xkrja
    wrote on last edited by
    #1

    I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:

    class MyListObject
    {
    int MyInt {get;set;}
    string MyString {get;set;}
    }

    And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:

    MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});

    And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!

    K S 2 Replies Last reply
    0
    • X xkrja

      I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:

      class MyListObject
      {
      int MyInt {get;set;}
      string MyString {get;set;}
      }

      And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:

      MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});

      And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!

      K Offline
      K Offline
      Keith Barrow
      wrote on last edited by
      #2

      You get a System.InvalidOperationException exception with the message "Collection was modified; enumeration operation may not execute.". To fix:

      lock (MyList)
      {
      MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});
      }

      This locks the object so that only the code block can modify / access it. If you put more than one lock around the object in different places, you need to think about race and deadlock conditions, but for your example it should be fine.

      Dalek Dave: There are many words that some find offensive, Homosexuality, Alcoholism, Religion, Visual Basic, Manchester United, Butter. Pete o'Hanlon: If it wasn't insulting tools, I'd say you were dumber than a bag of spanners.

      1 Reply Last reply
      0
      • X xkrja

        I would like to know what happens if objects are removed from a list while operations are performed on this list? Let's say we have this class:

        class MyListObject
        {
        int MyInt {get;set;}
        string MyString {get;set;}
        }

        And we have a list of "MyListObject" called "MyList". We also have an eventhandler that can trigger randomly. In that eventhandler a random item in "MyList" is removed from "MyList". Now, what happens if I do something like:

        MyList.Find(delegate (MyListObject mlo) {return mlo.MyString == "hello";});

        And at the same time we perform this operation on the list the event is triggered and the eventhandler removes some item from the list. Will that throw some kind of exception? I don't know how to test it so that's why I'm asking you guys. If this can cause problems, how can I get around this? Thanks for help!

        S Offline
        S Offline
        SeMartens
        wrote on last edited by
        #3

        Hi, at MSDN it is written that the enumeration is not thread-safe: http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx[^] As far as I know removing an item from an collection that is enumerated at the moment throws an Exception (something like an InvalidOperationException). So to solve this problem I suggest using the lock statement: http://msdn.microsoft.com/en-us/library/c5kehkcz%28VS.80%29.aspx[^] Lock the access when removing something from the list, and lock when you iterate through the list. (Be sure to use the same lock-object!) Hope this helps. Regards Sebastian

        It's not a bug, it's a feature! Check out my CodeProject article Permission-by-aspect. Me in Softwareland.

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups