Key-based I/O in .NET collections: which listed collections perform input/output based on a key?
-
AMap and Stack
-
BStack and BitArray
-
CMap, Stack, and BitArray
-
DHashtable and SortedList
-
EAll of the above
Answer
Correct Answer: Hashtable and SortedList
Explanation
Introduction / Context:Different collections provide different access semantics. Some are key-based (associative containers), while others are position-based or bit-based. Choosing the right collection is essential for clarity and performance.
Given Data / Assumptions:
- We consider classic System.Collections types.
- Hashtable and SortedList are associative containers keyed by objects.
Concept / Approach:Key-based collections allow storage and retrieval by key rather than by numeric index. Hashtable provides hash-based key lookup with no ordering guarantees. SortedList stores key–value pairs sorted by keys, enabling ordered iteration and binary-search-like retrieval.
Step-by-Step Solution:
Hashtable → key-based access (e.g., ht["id"]).SortedList → key-based access plus sorted by keys.Stack → LIFO structure, no keys, push/pop/peek operations.BitArray → compact boolean store accessed by integer index, not keys.'Map' → not a .NET Framework class name in System.Collections; the closest equivalents are DictionaryVerification / Alternative check:Create a Hashtable and a SortedList and insert/retrieve items via keys; attempt the same with Stack or BitArray and observe the absence of key-based APIs.
Why Other Options Are Wrong:They include non-keyed structures or non-existent types (as named).
Common Pitfalls:Assuming Stack has named access or that BitArray supports keys beyond integer indices.
Final Answer:Hashtable and SortedList