C#: Using IDictionaryEnumerator with HashTable

Posted by: Suprotim Agarwal , on 8/8/2011, in Category C#
Views: 108912
Abstract: In this article, we will see how to enumerate a HashTable using an IDictionaryEnumerator.

The IDictionaryEnumerator enumerates the elements of a non-generic dictionary like a HashTable. It is a strongly typed enumerator which inherits IEnumerator by adding three properties:


where Entry gets both the key and the value of the current dictionary entry in the form of a DictionaryEntry struct, Key gets the key of the current dictionary entry and Value gets the value of the current dictionary entry.

A HashTable collection stores a (Key, Value) pair and uses the Key to hash and obtain the storage location. Let us see how to enumerate a HashTable using an IDictionaryEnumerator



As you can see in the code above, we are using HashTable.GetEnumerator which iterates through the HashTable. Since the the enumerator is positioned before the first element in the collection, you must call the MoveNext method to advance the enumerator to the first element of the collection. When there are no more elements to read, MoveNext() returns false. The current value is obtained using Entry, Key and Value.

Note: You can also access items in the Dictionary using the generalized Entry property which returns a DictionaryEntry class type i.e. via ide.Entry.Key and ide.Entry.Value



While Enumerating a HashTable or a non-generic dictionary key value pair, use the IDictionaryEnumerator instead of the IEnumerator since the former gives you access to both the key and value of the current entry.

I hope this article was useful and I thank you for viewing it.

The entire source code of this article can be downloaded over here

This article has been editorially reviewed by Suprotim Agarwal.

Absolutely Awesome Book on C# and .NET

C# and .NET have been around for a very long time, but their constant growth means there’s always more to learn.

We at DotNetCurry are very excited to announce The Absolutely Awesome Book on C# and .NET. This is a 500 pages concise technical eBook available in PDF, ePub (iPad), and Mobi (Kindle).

Organized around concepts, this Book aims to provide a concise, yet solid foundation in C# and .NET, covering C# 6.0, C# 7.0 and .NET Core, with chapters on the latest .NET Core 3.0, .NET Standard and C# 8.0 (final release) too. Use these concepts to deepen your existing knowledge of C# and .NET, to have a solid grasp of the latest in C# and .NET OR to crack your next .NET Interview.

Click here to Explore the Table of Contents or Download Sample Chapters!

What Others Are Reading!
Was this article worth reading? Share it with fellow developers too. Thanks!
Share on LinkedIn
Share on Google+

Suprotim Agarwal, MCSD, MCAD, MCDBA, MCSE, is the founder of DotNetCurry, DNC Magazine for Developers, SQLServerCurry and DevCurry. He has also authored a couple of books 51 Recipes using jQuery with ASP.NET Controls and The Absolutely Awesome jQuery CookBook.

Suprotim has received the prestigious Microsoft MVP award for Sixteen consecutive years. In a professional capacity, he is the CEO of A2Z Knowledge Visuals Pvt Ltd, a digital group that offers Digital Marketing and Branding services to businesses, both in a start-up and enterprise environment.

Get in touch with him on Twitter @suprotimagarwal or at LinkedIn

Page copy protected against web site content infringement 	by Copyscape

Feedback - Leave us some adulation, criticism and everything in between!
Comment posted by Santhosh on Thursday, October 20, 2011 10:26 AM
It was good. Thanks.
Comment posted by Iyyappam on Monday, November 26, 2012 9:05 AM
Easy to understand the concept of IDictionaryEnumerator and Hash table.
Comment posted by Mahavirsinh Padhiyar on Tuesday, December 23, 2014 12:48 AM
IT throws an exception not display exception but it throws and result is not display properly. It has the values that I stored in Hashtable like above.
Please reply as soon as possible.
Comment posted by izharulhaq on Saturday, January 10, 2015 3:59 AM
dear sir:
  hope you are fine and doing well i want to make dictionary in my local language pashto would you help to make it in c# language if you would please inform me i am so interesting to make it