Hashtable in .NET: choose the correct combined statement about its characteristics.

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 is preferred today, many codebases still expose Hashtable.



Given Data / Assumptions:

  • We are discussing System.Collections.Hashtable (non-generic).
  • Focus areas: key-based access, ordering, indexing, and enumeration.


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:

Keyed nature → True: you look up by key (e.g., ht[""id""]).Ordering → False: no ordering guarantees; enumeration order is unspecified.Index-based → False: there is no positional integer index API.Enumerator → True: Hashtable’s enumerator implements IDictionaryEnumerator to yield DictionaryEntry (Key, Value).Keys/Values → Exposed on the Hashtable itself via the Keys and Values properties (collections), not only via the enumerator.


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 in modern generic code for type safety and performance.



Final Answer:
Hashtable is a keyed collection; it is unordered and not index-based; its enumerator type implements IDictionaryEnumerator.

More Questions from Collection Classes

Discussion & Comments

No comments yet. Be the first to comment!
Join Discussion