locked
How to iterate in a hashtable?

    Question

  •  
    Hi,

        Which is the best possible way to iterate through a hashtable?

    Thanks in advance
    Saturday, September 13, 2008 4:32 AM

Answers

  • Maybe it goes without saying, you can't change your Hashtable while enumerating through it with foreach.  It will throw an error.  Also, if you are multi-threading, foreach is not threadsafe; you'll need to synchronize your threads and lock access during the whole enumeration (or be prepared to catch an exception).

    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 11:47 AM
  • Best in what way?

    It's pretty easy to do using a plain old foreach loop.

    Mattias, C# MVP
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 8:35 AM
  • foreach is your friend. If you genuinely mean Hashtable, then each item yielded is a DictionaryEntry:

                foreach (DictionaryEntry pair in table)
                {
                    Console.WriteLine("{0}={1}", pair.Key, pair.Value);
                }

    This gets easier with .NET 2.0 and the generic IDictionary<TKey,TKey>, since it is more obviously typed - though as KeyValuePair<TKey,TValue>. Fortunately C# 3.0 lets you forget about this mouthful:

                foreach (var pair in table)
                {
                    Console.WriteLine("{0}={1}", pair.Key, pair.Value);
                }

    In either case, another option is to enumerate the Keys, and use the indexer to get the values for each - but enumerating the pairs should be a little more efficient.

    Marc
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 11:23 AM

All replies

  • Best in what way?

    It's pretty easy to do using a plain old foreach loop.

    Mattias, C# MVP
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 8:35 AM
  • foreach is your friend. If you genuinely mean Hashtable, then each item yielded is a DictionaryEntry:

                foreach (DictionaryEntry pair in table)
                {
                    Console.WriteLine("{0}={1}", pair.Key, pair.Value);
                }

    This gets easier with .NET 2.0 and the generic IDictionary<TKey,TKey>, since it is more obviously typed - though as KeyValuePair<TKey,TValue>. Fortunately C# 3.0 lets you forget about this mouthful:

                foreach (var pair in table)
                {
                    Console.WriteLine("{0}={1}", pair.Key, pair.Value);
                }

    In either case, another option is to enumerate the Keys, and use the indexer to get the values for each - but enumerating the pairs should be a little more efficient.

    Marc
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 11:23 AM
  • Maybe it goes without saying, you can't change your Hashtable while enumerating through it with foreach.  It will throw an error.  Also, if you are multi-threading, foreach is not threadsafe; you'll need to synchronize your threads and lock access during the whole enumeration (or be prepared to catch an exception).

    Les Potter, Xalnix Corporation, Yet Another C# Blog
    • Marked as answer by jack 321 Wednesday, September 17, 2008 3:28 AM
    Monday, September 15, 2008 11:47 AM
  • The below mentioned codes will explain this better:
    esired 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 .
     

    Cheers, Eliza
    Thursday, March 18, 2010 7:28 AM