Apparently, I answered that question a long time ago (and I am a little
ashamed of it, because there are a few obvious errors in it).
If you need to clear all elements, call the Clear method.
If you need to clear only specific elements, you can do this:
public static int RemoveAll(Hashtable hashtable, Predicate<DictionaryEntry>
predicate)
{
// The number of items removed.
int retVal = 0;
// Create space for the dictionary entries.
DictionaryEntry[] dictionaryEntries = new
DictionaryEntry[hashtable.Count];
// Copy the keys.
hashtable.CopyTo(dictionaryEntries, 0);
// Iterate through the keys.
foreach (DictionaryEntry dictionaryEntry in dictionaryEntries)
{
// If the predicate is true, remove the item.
if (predicate(dictionaryEntry))
{
// Remove the item.
hashtable.Remove(dictionaryEntry.Key);
// Increment the count.
retVal ++;
}
}
// That's all folks.
return retVal;
}
The good thing about this is that in Orcas, you could place a "this" in
front of the Hashtable parameter and it will then be an extension method.
--
- Nicholas Paldino [.NET/C# MVP]
-
mv*@spam.guard.caspershouse.com
"Mr. Arnold" <MR.
Ar****@Arnold.comwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
>
"Jen" <no**@nowhere.comwrote in message
news:O$**************@TK2MSFTNGP05.phx.gbl...
>The usual trick to get around the "collection was modified" problem by
looping through the collection backwards by index won't work with a
Hashtable because there is no way to access by index, right? How do I
loop through a Hashtable to selectively remove items?
Here are some examples.
http://www.thescripts.com/forum/thread256383.html