From: eliza on
If we write in the same way as above for the hashtable then we would not get the desired values.

Hashtable ss = new Hashtable();
ss["key1"] ="india";
ss["key2"] = "bharat";

foreach (object gg in ss)
{
Console.WriteLine("Key value is " + gg);
Console.Read();
}

Here we get System.Collections.DictionaryEntry and System.Collections.DictionaryEntry as output instead of the value pairs stored in the hashtable.

In this case we can help of DictinaryEntry object for iterating the hashtable.

foreach(DictionaryEntry gg in ss)
{
Console.WriteLine("Key and value are " + gg.Key + " " + gg.Value);
Console.Read();
}

A DictionaryEntry object is simply a container containing the Key and Value .

http://www.mindfiresolutions.com/How-To-Iterate-the-Hashtable-in-C-311.php



Jen wrote:

Removing items from a Hashtable in a loop?
19-May-07

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?

Previous Posts In This Thread:

On Saturday, May 19, 2007 6:52 PM
Jen wrote:

Removing items from a Hashtable in a loop?
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?

On Saturday, May 19, 2007 8:37 PM
arn wrote:

Re: Removing items from a Hashtable in a loop?
Jen wrote:

Save keys that should be removed in an ArrayList while looping
through and remove all keys from that after.

Arne

On Saturday, May 19, 2007 11:16 PM
Mr. Arnold wrote:

Re: Removing items from a Hashtable in a loop?
Here are some examples.

http://www.thescripts.com/forum/thread256383.html

On Saturday, May 19, 2007 11:53 PM
Jen wrote:

Yikes.
Yikes. Couldn't the designers of the collection classes have handled this
better??

On Sunday, May 20, 2007 6:25 AM
Jesse Houwing wrote:

List has a very nice RemoveAll function which you can pass a delegate which
List has a very nice RemoveAll function which you can pass a delegate
which tells which ones to remove and which ones to keep. I don't see
such a function in Dictionary however. Too bad, they should have added that.

Jesse

* Jen wrote, On 20-5-2007 5:53:

On Sunday, May 20, 2007 12:36 PM
Nicholas Paldino [.NET/C# MVP] wrote:

Apparently, I answered that question a long time ago (and I am a little
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]
- mvp(a)spam.guard.caspershouse.com



"Mr. Arnold" <MR. Arnold(a)Arnold.com> wrote in message
news:%237UVS1omHHA.1244(a)TK2MSFTNGP04.phx.gbl...


Submitted via EggHeadCafe - Software Developer Portal of Choice
BizTalk Custom Pipeline for Splitting Messages
http://www.eggheadcafe.com/tutorials/aspnet/a0786aaf-c159-40ff-9cf6-241d5e325f42/biztalk-custom-pipeline-f.aspx
From: Arne Vajhøj on
On 18-03-2010 03:51, eliza wrote:
> If we write in the same way as above for the hashtable then we would not get the desired values.

You just replied to an almost 3 year old post.

If you do that then it better be an extremely interesting content.

Iterating a Hashtable is something most readers already will
know how to do.

If you happen to read this, then please consider for the future.

Arne
From: Rob on
On Thu, 18 Mar 2010 21:27:26 -0400, Arne Vajh�j <arne(a)vajhoej.dk>
wrote:

>On 18-03-2010 03:51, eliza wrote:
>> If we write in the same way as above for the hashtable then we would not get the desired values.
>
>You just replied to an almost 3 year old post.

That is impressive. I wonder which news service keeps posts for 3
years...?
From: Arne Vajhøj on
On 19-03-2010 15:05, Rob wrote:
> On Thu, 18 Mar 2010 21:27:26 -0400, Arne Vajh�j<arne(a)vajhoej.dk>
> wrote:
>> On 18-03-2010 03:51, eliza wrote:
>>> If we write in the same way as above for the hashtable then we would not get the desired values.
>>
>> You just replied to an almost 3 year old post.
>
> That is impressive. I wonder which news service keeps posts for 3
> years...?

Probably not any NNTP server.

But some web interfaces does.

Google Groups or as in this case EggHeadCafe.

Arne