Attention: We are retiring the ASP.NET Community Blogs. Learn more >

Enumerating Hashtables

A lengthy thread was started about enumerating a hashtable and modifying the values.  The individual was getting the standard modification within a foreach exception thrown.  I had suggested the following code and I seem to have the shortest and easiest method in doing so...

ArrayList arrayList = new ArrayList(hashtable.Keys);
IEnumerator listEnumerator = arrayList.GetEnumerator();
while (listEnumerator.MoveNext()) {
    hashtable[listEnumerator.Current] = "value";
} // while

Is there a better or recommended way to do this?  The above seemed right to me.

UPDATE:  Jerry Pisk had suggested that I was doing extra code when you can just enumerate using foreach and modify the values.  That was the whole point of me writing the first paragraph above Jerry.  Doing that causes an exception.  You can enumerate all day long and “read” values, but you can not modify the values within the foreach loop.  Which brings you to the code I wrote above...

12 Comments

  • Why do you create an extra ArrayList object? ICollection is enumerable, you can enumerate the Keys directly instead of creating an extra ArrayList object:



    foreach( Object iKey in hashtable.Keys )

    {

    hashtable[iKey] = "value";

    }



    It's faster and uses less memory (since it doesn't copy the keys collection into an ArrayList).

  • Did you even try the code you are suggesting? Will not work Jerry ;)

  • Very nice, though I'd personally just an array:



    object[] array = new object[hashtable.Keys.Count];

    hashtable.Keys.CopyTo(array, 0);

    foreach (object key in array) {

    hashtable[key] = "value";

    }



    Using a foreach here does work, right? Is there a reason you used a while statement?

  • And that, folks, is why you should never respond to blog posts right after waking up.

  • Ok, I think this is a bug in Hashtable implementation as we do NOT modify the keys collection by assigning an existing key new value. This whole thread is a nice example of developers trying to work around a bug.

  • This might not be the most performant variant, but I think it is the most readable:



    foreach( object key in new ArrayList(hashtable.Keys) )

    {

    hashtable[key] = "value";

    }

  • How about:



    IDictionaryEnumerator myEnum = hashtable.GetEnumerator();



    while(myEnum.MoveNext())

    {

    hashtable[myEnum.Key] = "value";

    }



  • We have Hashtable:



    Rule Flow (r1,r2) is an arraylist..



    Ruleid Rule Flow (arraylist)



    R01 r1, r2, r3, r4



    R02 r5, r6



    How to extract the values of r1, r2, r3, r4 using Rule

    id in Hashtable



    Can u suggest something!!!





  • We have Hashtable:



    Rule Flow (r1,r2) is an arraylist..



    Ruleid Rule Flow (arraylist)



    R01 r1, r2, r3, r4



    R02 r5, r6



    How to extract the values of r1, r2, r3, r4 using Rule

    id in Hashtable



    Can u suggest something!!!





  • I am having a hard time understanding your post. Could you please be more clear or post some sample code of what you are tyring to accomplish.



    Thanks

  • If you just want to loop through, you can also try

    foreach (DictionaryEntry item in hash)
    {
    item.Value = "value";
    }

    isn't it quite a clean code?

  • @Wander:

    Did you try that code? Cause it will not work.

Comments have been disabled for this content.