Can anyone tell me why is a IDictionaryEnumerator preferred for looping a HashTable.
How is it different from IEnumerator although it is derived from it?
When to use which?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
IDictionaryEnumerator allows you to access both the key and the value of the current entry. As you note, an IDictionaryEnumerator is an IEnumerator, so if you’re just using it in a foreach, you’ll only notice this because the range variable is a DictionaryEntry; but if you’re working with the IDictionaryEnumerator manually, you’ll find it has Key and Value properties as well as Current.
It’s not so much that it is ‘preferred’ for looping a Hashtable — it’s just that if you enumerate a Hashtable, you’re enumerating key-value pairs, not just values, and the IDictionaryEnumerator represents that.