Difficulty: Easy
Correct Answer: Hashtable is a keyed collection; it is unordered and not index-based; its enumerator type implements IDictionaryEnumerator.
Explanation:
Introduction / Context:
Understanding core collection types is crucial for writing efficient C# code. Hashtable is one of the classic non-generic collections; while generic Dictionary
Given Data / Assumptions:
Concept / Approach:
Hashtable stores entries as key–value pairs and provides O(1)-average access by key. It does not guarantee any ordering of elements (neither insertion order nor sort order). It is not index-based (you do not access elements by numeric index). Iteration is provided via an enumerator that implements IDictionaryEnumerator, exposing Entry, Key, and Value of the current element.
Step-by-Step Solution:
Verification / Alternative check:
Create a Hashtable, add elements, iterate with foreach (DictionaryEntry de in ht) to access de.Key/de.Value. Try to rely on order; results will vary.
Why Other Options Are Wrong:
B claims insertion order and index-based access—incorrect. C says you cannot enumerate—incorrect. D forbids heterogeneous values—Hashtable stores objects; heterogeneity is allowed. E misstates where Keys/Values live; they are properties on Hashtable.
Common Pitfalls:
Assuming predictable order, or thinking it behaves like a List. Prefer Dictionary
Final Answer:
Hashtable is a keyed collection; it is unordered and not index-based; its enumerator type implements IDictionaryEnumerator.
Discussion & Comments