Dictionary, Enumeration and Remove

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dave Arkley

    #1

    Dictionary, Enumeration and Remove

    I have a type

    Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>();

    Im populating the dictionary so that a key is associated with a list of
    integers. I then remove some, or possible all integers from the list by using
    foreach:

    foreach (int i in oKP.Keys)
    {
    foreach (int j in oKP[i])
    {
    if (oKP.ContainsKe y(j))
    {
    if (oKP[j].Contains(i)) oKP[j].Remove(i);
    }
    }
    }

    I'd like to add a test which determines if the list is now empty
    (oKP[i].Count == 0) and if so deletes the key and list from the dictionary -
    like so:

    foreach (int i in oKP.Keys)
    {
    foreach (int j in oKP[i])
    {
    if (oKP.ContainsKe y(j))
    {
    if (oKP[j].Contains(i)) oKP[j].Remove(i);
    if (0 == oKP[j].Count) oKP.Remove(j);
    }
    }
    }

    However when the remove of the dictionary entry is actioned I get an error:

    Collection was modified; enumeration operation may not execute.

    Now I understand why this happens, my question is how do I iterate over the
    Dictionary and remove entries with a count of 0? What alternative can I use
    to foreach that allows me to delete an entry?

    Thanks in advance
  • Marina

    #2
    Re: Dictionary, Enumeration and Remove

    This is because you are not supposed to alter the collection while going
    through it using an enumerator (which is what for each is doing behind the
    scenes).
    Solution: use a for or while loop.

    "Dave Arkley" <Dave Arkley@discussi ons.microsoft.c om> wrote in message
    news:E7315142-824A-43AF-931E-A11AE39A8926@mi crosoft.com...[color=blue]
    > I have a type
    >
    > Dictionary<int, List<int>> oKP = new Dictionary<int, List<int>>();
    >
    > Im populating the dictionary so that a key is associated with a list of
    > integers. I then remove some, or possible all integers from the list by[/color]
    using[color=blue]
    > foreach:
    >
    > foreach (int i in oKP.Keys)
    > {
    > foreach (int j in oKP[i])
    > {
    > if (oKP.ContainsKe y(j))
    > {
    > if (oKP[j].Contains(i)) oKP[j].Remove(i);
    > }
    > }
    > }
    >
    > I'd like to add a test which determines if the list is now empty
    > (oKP[i].Count == 0) and if so deletes the key and list from the[/color]
    dictionary -[color=blue]
    > like so:
    >
    > foreach (int i in oKP.Keys)
    > {
    > foreach (int j in oKP[i])
    > {
    > if (oKP.ContainsKe y(j))
    > {
    > if (oKP[j].Contains(i)) oKP[j].Remove(i);
    > if (0 == oKP[j].Count) oKP.Remove(j);
    > }
    > }
    > }
    >
    > However when the remove of the dictionary entry is actioned I get an[/color]
    error:[color=blue]
    >
    > Collection was modified; enumeration operation may not execute.
    >
    > Now I understand why this happens, my question is how do I iterate over[/color]
    the[color=blue]
    > Dictionary and remove entries with a count of 0? What alternative can I[/color]
    use[color=blue]
    > to foreach that allows me to delete an entry?
    >
    > Thanks in advance[/color]


    Comment

    Working...