removing an item from a dictionary

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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 through a dictionary, looking for
things, then remove them....

John
 
Here's one way:

Dim Keys(myDictionary.Count - 1) As Object

For i As Integer = 0 to myDictionary.Count - 1
myDictionary.Remove(Keys.GetValue(i))
Next i

hope that helps..
Imran.
 
added info, my key to the dictionary is of type string, so I'm lost how to
interate, such that I can remove while I loop. I'm in C# if that helps
 
....and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}

hope that helps..
Imran.
 
Imran Koradia said:
...and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}

That's a bad way of doing it, IMO (and won't even compile in its
current form). There's no guarantee that Keys will maintain the
original order when items are removed from it.

I would suggest creating a collection to contain the keys you wish to
remove, and after you've found everything you want to remove, you then
remove them:

ArrayList removals = new ArrayList();

foreach (object key in myDictionary.Keys)
{
if (someCondition)
{
removals.Add(key);
}
}

foreach (object key in removals)
{
dictionary.Remove(key);
}
 
ok..looks like I was half asleep. I forgot the CopyTo in the original VB
piece of which I then simply translated using one of the online tools. And
yes - it doesn't even compile - my apologies :(.

So here's the code again in VB:

Dim Keys(myDictionary.Count - 1) As Object
myDictionary.Keys.CopyTo(Keys, 0)

For i As Integer = 0 To myDictionary.Count - 1
myDictionary.Remove(Keys(i))
Next

The C# code looks like (which now compiles and also runs :)):
object[] Keys = new object[myDictionary.Count];
myDictionary.Keys.CopyTo(Keys, 0);
int count = myDictionary.Count;
for (int i = 0; i <= count-1; i++) {
myDictionary.Remove(Keys);
}

Of course, you can always add conditions before removing the items. You
should be able to get the Item by myDictionary.Item(Keys) if you need to
check a condition on the item before removing it.


hope that helps..
Imran.

Jon Skeet said:
Imran Koradia said:
...and here's your C# version:

object[myDictionary.Count - 1] Keys;
for (int i = 0; i <= myDictionary.Count - 1; i++) {
myDictionary.Remove(Keys.GetValue(i));
}

That's a bad way of doing it, IMO (and won't even compile in its
current form). There's no guarantee that Keys will maintain the
original order when items are removed from it.

I would suggest creating a collection to contain the keys you wish to
remove, and after you've found everything you want to remove, you then
remove them:

ArrayList removals = new ArrayList();

foreach (object key in myDictionary.Keys)
{
if (someCondition)
{
removals.Add(key);
}
}

foreach (object key in removals)
{
dictionary.Remove(key);
}
 
Back
Top