Collections

You may have previously known collections by a different name: generic containers. Although in C#, there will be some that are "weakly typed", meaning they will only contain object types. They're functionally similar to our vector, ArrayList, Map<Key, Value>, and BinTree from previous classes and languages. Generally speaking, they exist as alternatives to arrays, while often being superior to them in terms of efficiency or easy-of-use, depending on the application. So while you can theoretically live your entire life only using arrays, it would be akin to living your entire life without ever using a vehicle operated by a combustible engine. There are some programmer things you'll do that are just much easier to accomplish if you can intelligently leverage a List collection, or a Set collection.

All collections have the IEnumerable implemented, meaning we can use the foreach mechanism on any of them. Some will have a Sort method implemented as well, which utilizes the IComparable interface, while others will maintain sorted order as items are added to the collection (see: SortedList or SortedSet, described below). If you're using a List of Student objects, for example, you'll need to make sure you have the IComparable interface implemented for this class. You can also convert any of them into a similarly-typed array using the CopyTo method. Here's a short list of some of the more common collections you'll typically use.

  • List<T>. Functionally, an array. Not to be confused with...

  • LinkedList<T>. A doubly-linked list of Nodes.

  • Stack<T>. Your run-of-the-mill, LIFO stack container.

  • Queue<T>. Your run-of-the-mill, FIFO queue container.

  • Dictionary<TKey, TValue>. The "T" before Key and Value mean that their data type is generic. This is functionally a Map or HashMap container from other programming languages.

  • SortedList<TKey, TValue>. This one's unusual. Despite the name, this is functionally more similar to a sorted Dictionary (which is implemented with SortedDictionary), as the element pairs of this container are continuously sorted by their TKey values. SortedList collections use less memory, but don't insert/remove as quickly as SortedDictionaries, but are superior when initialized with all the data at once. Not to be confused with...

  • SortedSet<T>. Which is functionally similar to a LinkedList collection, that (1) inserts new elements to maintain sorted order and (2) prohibits the inclusion of duplicate entries.

  • HashSet<T>. Which is a high-performance set container (without ordering the elements within), most useful when you are leveraging mathematical set operations, such as unions and intersections. These operations will change the original HashSet, as opposed to many other types of set operations which return a new set of results (or an empty set), based on the operation.

Many of these collections have the methods and properties you've come to expect from these types. "Capacity" is a property that defines how many elements can be presently stored without re-sizing, while "Count" is the property that gives how many elements are currently being stored (it would have been entirely too convenient to use a more convention term here, like "Size" *grumble grumble*...). You can sometimes access items within a container using a "Item[Int32 index]" property, dependent on the structure of the container you're using. There isn't too great a use for subscript notation when using a stack or queue.

There are methods implemented to add elements, remove elements, sort, search, clear, etc. You may refer to MSDN documentation pages for a complete list of properties/methods for each container.