Manage a Group of Associated Data by Using Collections
Before taking any step forward, please note that the collection classes presented here are obsolete. This is because they have generic counterparts, with which we’ll deal later. For the exam, you should know these classes, but in the real life, use generics instead.
First let’s see the interfaces which will be useful when defining our custom collection classes: IEnumerator and IEnumerable. These interfaces are the only way to iterate over your data in a foreach loop. You decorate your class (or struct) with the IEnumerable interface, which has a method named GetEnumerator, of type IEnumerator. Note that you can use a foreach loop without implementing IEnumerable. Simply create a method in your class with the name of GetEnumerator, and the type of IEnumerator. Then you can make use of the yield keyword, which returns data, and stores the position of the index. Syntax:
public class EnumExample
public IEnumarator GetEnumerator()
yield return “Hello!”;
yield return “World!”;
It’s worth knowing the methods of IEnumerator, namely: Current of type Object, MoveNext which returns true until there is a next object, and Reset, which positions the index on the first item. You can find additional information in the link list below the article.
There are other interfaces related to the collection classes. Let’s take a brief look:
- ICollection: extends IEnumerable. It adds count, copying and synchronization methods.
- IList: extends ICollection, adds inserting, removing methods for it.
- IComparer: adds custom comparing functionality for sorting and ordering.
- IEqualityComparer: adds custom equality checking depending on the hash of an object.
- IDictionaryEnumerator: extends IEnumerator with key-value accessing functionality.
Now we’ve seen what makes them work, let’s examine the collection classes one by one, provided by the .NET Framework.
- CollectionBase: CollectionBase provides an abstract, modifiable base class for your custom collections. It implements the IList interface. The purpose of this class is to provide developers a starting point to create collections.
- ReadOnlyCollectionBase: this class provides the same functionality as CollectionBase. The main difference is that it’s read-only. It implements the interfaces ICollection and IEnumerable. Use this class to create a read-only collection.
- DictionaryBase: provides an abstract class to extend and create custom dictionary (key-value) based collections. Implements the interfaces IDictionary, IEnumerable, ICollection.
- ArrayList: ArrayList implements ICollection, IList, IEnumerable and IClonable (later). It provides a way to create dynamic collections, as its size grows to store the elements added to it. This means the initial size is 0. It has sorting capabilities, but it’s not guaranteed to be sorted, so call explicitly the Sort method before work with the values.
- HashTable: represents a key-value collection organized by the hash-code of the key. Implements the interfaces IDictionary, ICollection, IEnumerable. It’s also serializable. Key-value pairs are stored as DictionaryEntries. HashTable increases its size dynamically.
- Queue: implements ICollection, IEnumerable. Represents a first-in, first-out (FIFO) collection of objects. Use the methods Enqueue and Dequeue to add or retrieve items from the Queue. You can also use the Peek method to return the first object in the queue without removing it.
- Stack: implements ICollection and IEnumerable. Represents a first-in, last-out (LIFO) collection of objects. Methods are Push and Pop. You can also use the Peek method to return the object on the top without removing it.
- SortedList: IDictionary, ICollection, IEnumerable. Represents key-value pairs as a list. You can access these items by their index, or the key specified. It stores to arrays for the keys and the values. Each item is a DictionaryEntry. Sorted by the key values (or custom logic).
- BitArray: ICollection, IEnumerable. Manages a compact array of Boolean values. True indicates that the bit is on, false off.