473,396 Members | 1,797 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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.ContainsKey(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.ContainsKey(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
Nov 22 '05 #1
1 5177
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 Ar****@discussions.microsoft.com> wrote in message
news:E7**********************************@microsof t.com...
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.ContainsKey(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.ContainsKey(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

Nov 22 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dave Arkley | last post by:
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...
6
by: JohnK | last post by:
ok, ya got me here. I'm trying to removing items from a dictionary inside a loop. Obviously using enumeration does not work as that assumes the dictionary stays unchanged. So how so I iterate...
2
by: orekinbck | last post by:
Hi There I am probably missing something fundamental here, but I cannot see a method to search the values of a generic dictionary so that I can find the key ? Of course I could enumerate...
1
by: john wright | last post by:
I have a dictionary oject I created and I want to bind a listbox to it. I am including the code for the dictionary object. Here is the error I am getting: "System.Exception: Complex...
1
by: Martin Widmer | last post by:
Hi Folks. When I iterate through my custom designed collection, I always get the error: "Unable to cast object of type 'System.Collections.DictionaryEntry' to type...
1
by: Dan Holmes | last post by:
I have a custom dictionary and i would like to bind it to a control. Since Dictionary doesn't implement IList i would have to write it. Is the only way to do this to keep another collection that...
2
by: tamara.roberson | last post by:
I am porting some code to use generics. Currently, we have a synchronized Hashtable called as follows: private Hashtable table = Hashtable.Synchronized (new Hashtable ()); What I would like...
4
by: O.B. | last post by:
I need the ability to parse through the values of a Dictionary and remove certain ones depending on their attribute values. In the example below, an InvalidOperationException is thrown in the...
6
by: GiJeet | last post by:
hello, I'm trying to use a dictionary as a class member. I want to use a property to get/set the key/value of the dictionary but I'm confused as how to use a dictionary as a property. Since there...
8
by: Bob Altman | last post by:
Hi all, I'm trying to do something that should be really easy, but I can't think of an obvious way to do it. I have a dictionary whose value is a "value type" (as opposed to a reference type --...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.