System.Collections Namespace

The System.Collections namespace contains interfaces and classes that define non-generic collections of objects. These collections can store any type of object, including primitive types, value types, and reference types.

Introduction

The System.Collections namespace provides fundamental collection types that were essential before the introduction of generics in .NET 2.0. While generic collections (found in System.Collections.Generic) are generally preferred for type safety and performance, the non-generic collections still have their uses, especially when dealing with older code or when working with `ArrayList` and `Hashtable`.

Key concepts include:

  • Storing objects of type object.
  • Manual type casting is often required when retrieving elements.
  • Less type safety compared to generic collections.

Interfaces

This namespace defines several important interfaces for collection management:

  • ICollection: Represents a collection of objects. It is the base interface for many collection types.
  • IEnumerable: Represents the ability to enumerate through a collection.
  • IEnumerator: Represents an enumerator for a collection, allowing iteration over its elements.
  • IList: Represents a collection that can be accessed by index, supporting adding, removing, and modifying elements at specific positions.
  • IDictionary: Represents a collection of key/value pairs.
  • IDictionaryEnumerator: Represents an enumerator for a dictionary.
  • IHashCodeProvider: (Obsolete) Provides a hash code for an object.
  • ICloneable: Defines a method that supports copying the collection.

Classes

Some of the primary classes within this namespace include:

  • ArrayList: A dynamic array that can grow as needed. It stores objects and requires casting.
  • Hashtable: A collection of key/value pairs that are organized based on hash codes.
  • Queue: A collection of objects that represents a first-in, first-out (FIFO) queue.
  • Stack: A collection of objects that represents a last-in, first-out (LIFO) stack.
  • SortedList: A collection of key/value pairs that is sorted by keys and is accessible by index.
  • DictionaryEntry: Represents an entry in a dictionary (key/value pair).
  • CollectionBase: An abstract base class that provides a simple implementation of the ICollection interface.
  • DictionaryBase: An abstract base class that provides a simple implementation of the IDictionary interface.

Common Collection Types

ArrayList

An ArrayList is a resizable array of objects. It's similar to an array but can dynamically resize itself as elements are added or removed.

using System.Collections; // ... ArrayList myList = new ArrayList(); myList.Add("Hello"); myList.Add(123); // Stores as object string firstItem = (string)myList[0]; // Requires casting int secondItem = (int)myList[1]; // Requires casting

Hashtable

A Hashtable stores key-value pairs. Keys must be unique. It uses hashing to provide efficient lookups.

using System.Collections; // ... Hashtable myCache = new Hashtable(); myCache["key1"] = "Value 1"; myCache[123] = "Value for integer key"; string value = (string)myCache["key1"];

Queue

A Queue implements the First-In, First-Out (FIFO) principle. Elements are added to the end and removed from the beginning.

using System.Collections; // ... Queue myQueue = new Queue(); myQueue.Enqueue("First"); myQueue.Enqueue("Second"); string firstIn = (string)myQueue.Dequeue(); // "First"

Stack

A Stack implements the Last-In, First-Out (LIFO) principle. Elements are added to and removed from the top.

using System.Collections; // ... Stack myStack = new Stack(); myStack.Push("Bottom"); myStack.Push("Top"); string topElement = (string)myStack.Pop(); // "Top"

For type-safe and more performant collections, refer to the System.Collections.Generic namespace.