The .NET Framework supports a variety of collection classes that can be used in different circumstances. Here are some points that give a quick summary of Collections and Genrics in framework 2.0
Basics
- The ArrayList is a simple collection of unordered items.
- The Add and AddRange methods of the ArrayList are used to add items to an ArrayList.
- The Insert and InsertRange methods of the ArrayList are used to insert items into specific places in a collection.
- The Remove, RemoveAt, and RemoveRange methods of the ArrayList are used to delete items from a collection.
- The indexer of the ArrayList can be used to iterate over a collection.
- The IEnumerable and IEnumerator interfaces can be used to enumerate through a collection as well.
- The foreach construct in Visual Basic and C# use the IEnumerable interface to concisely iterate over a collection.
Sequential Lists
- The .NET Framework supports the Queue and Stack classes to provide collections that represent sequential lists of items.
- The Queue is a first-in, first-out (FIFO) collection.
- The Queue class supports the Enqueue and Dequeue methods for adding and removing items from the collection.
- The Stack is a last-in, first-out (LIFO) collection.
- The Stack class supports the Push and Pop methods for adding and removing items, respectively, from the collection.
- Both sequential collection classes support Peek for viewing the next item in the collection without removing it.
Dictionaries
- The IDictionary interface provides the basic calling convention for all Dictionary collections.
- The Hashtable class can be used to create lookup tables.
- You can use a DictionaryEntry object to get at the key and value of an object in a Dictionary collection.
- The SortedList can be used to create list of items that can be sorted by a key.
- The IEqualityComparer can be used to construct hash values and compare values for classes in an arbitrary way.
Specialized Collections
- The BitArray class and the BitVector32 structure can both be used to perform bit-wise operations on a series of Boolean values.
- The StringCollection and StringDictionary classes are type-safe classes for storing strings.
- You can create case-insensitive versions of Hashtable and SortedList objects using the CollectionUtil class.
- NameValueCollection is a useful class for storing more than one value per key in a name/value collection.
Generic Collections
- Generic collections can be used to create more type-safe and potentially faster versions of their nongeneric counterparts.
- The generic List, Dictionary, Queue, Stack, SortedList, and SortedDictionary classes are type-safe versions of the collections.
- The new LinkedList generic class is a collection for storing items that know about their own relationship in the list, and it allows for iteration without having access to the collection itself.
Advertisement