Chapter 14: Generics and Collections
Quiz Solutions
Solution to Question 14-1. Indexers are unnamed. You use the this keyword to create an indexer:
public string this[int index]
Solution to Question 14-2. Any type can be used, although it’s most common to use integers.
Solution to Question 14-3. The elements of the collection that you want to sort must implement IComparable.
Solution to Question 14-4. Generics allow you to create type-safe collections without specifying the type the collection will hold when you create the collection.
Solution to Question 14-5. The IEnumerable<T> interface allows your collection to support a foreach loop.
Solution to Question 14-6. The purpose of the yield keyword is to return a value to the IEnumerator object, within the GetEnumerator( ) method.
Solution to Question 14-7. The size of an array is fixed when you create it. A List<T> expands dynamically when you add more elements.
Solution to Question 14-8. The Capacity property of a List indicates the number of elements that the List has room for. The Capacity is increased automatically when more elements are added.
Solution to Question 14-9. A Stack is a “last-in, first-out” collection, and a Queue is a “first-in, first-out” collection. In a Queue, elements are removed in the same order they were inserted. In a Stack, elements are removed in the opposite order.
Solution to Question 14-10. The key in a Dictionary takes the place of an indexer, and allows you to retrieve the associated value. The key can be ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access