Programming in C# — Question 154
You are developing an application that includes a class named Order. The application will store a collection of Order objects.
The collection must meet the following requirements:
✑ Internally store a key and a value for each collection item.
✑ Provide objects to iterators in ascending order based on the key.
✑ Ensure that item are accessible by zero-based index or by key.
You need to use a collection type that meets the requirements.
Which collection type should you use?
Answer options
- A. LinkedList
- B. Queue
- C. Array
- D. HashTable
- E. SortedList
Correct answer: E
Explanation
The correct answer is SortedList because it maintains items in sorted order based on keys and allows access by both index and key. LinkedList and Queue do not provide indexed access, while Array does not support key-value pairs or automatic sorting. HashTable allows key access but does not maintain any specific order.