The SortedList collection is a combination of the Array and HashTable classes. It maintains a list of items that can be accessed either with an index or with a key. When you access items by their indices, the SortedList behaves just like an ArrayList; when you access items by their keys, the SortedList behaves like… [Continue Reading]
Storing Data in Collections
Working with Keys and Values
Let’s look now at a few methods for extracting keys and values. To find out the index of a value in the SortedList, use the IndexOfValue method, which accepts an object as an argument. If the object exists in the collection, it returns its index; if not, it returns the value −1. If the same… [Continue Reading]
The IEnumerator and IComparer Interfaces
IEnumerator and IComparer are two classes that unlock some of the most powerful features of collections. The proper term for IEnumerator and IComparer is interface, a term I will describe shortly. Every class that implements the IEnumerator interface is capable of retrieving a list of pointers for all the items in a collection, and you… [Continue Reading]
Enumerating Collections
All collections expose the GetEnumerator method. This method returns an object of the IEnumerator type, which allows you to iterate through the collection without having to know anything about its items, not even the count of the items. To retrieve the enumerator for a collection, call its GetEnumerator method by using a statement like the… [Continue Reading]
Custom Sorting – Sort Method
The Sort method allows you to sort collections, as long as the items are of the same base data type. If the items are objects, however, the collection doesn’t know how to sort them. If you want to sort objects, you must help the collection a little by telling it how to compare the objects…. [Continue Reading]
Generic Collections
All collections we examined so far were designed to store objects because they should accommodate all data types, built-in or custom. In most practical situations, however, we want to make sure that collections store elements of the same type, just like arrays that were declared with a specific type. Why not be able to declare… [Continue Reading]