System.Collections Namespace

The System.Collections namespace provides interfaces and classes that define collections of objects. These include lists, queues, stacks, hash tables, and dictionaries. This namespace is fundamental for managing groups of related objects in .NET applications.
Core Interfaces

IEnumerable Interface

Supports iteration over a non-generic collection.

This interface is the base interface for all non-generic collections. It provides a single method, GetEnumerator, which returns an enumerator for the collection.

ICollection Interface

Represents a strongly typed collection of objects that can be accessed by index.

This interface inherits from IEnumerable and adds methods to count elements, copy them to an array, and check if the collection is synchronized.

IList Interface

Represents a non-generic collection of objects that can be individually accessed by index.

This interface inherits from ICollection and provides methods for inserting, removing, and searching for elements by index.

Common Non-Generic Collections

ArrayList Class

Represents a strongly typed list of objects that can be accessed by index. Provides methods for searching, sorting, and manipulating lists.

ArrayList is a dynamic array that can grow or shrink as needed. It stores elements of type Object.

using System.Collections;

ArrayList myArrayList = new ArrayList();
myArrayList.Add("Hello");
myArrayList.Add(123);
myArrayList.Add(true);

Console.WriteLine("Count: " + myArrayList.Count);
Console.WriteLine("First element: " + myArrayList[0]);

foreach (object item in myArrayList)
{
    Console.WriteLine(item);
}

Hashtable Class

Represents a collection of key and value pairs that are organized by hash code.

Hashtable provides fast lookups based on keys. It stores elements of type Object and requires unique keys.

using System.Collections;

Hashtable myHashtable = new Hashtable();
myHashtable["apple"] = "A red fruit";
myHashtable["banana"] = "A yellow fruit";

Console.WriteLine(myHashtable["apple"]);

foreach (DictionaryEntry entry in myHashtable)
{
    Console.WriteLine("Key: {0}, Value: {1}", entry.Key, entry.Value);
}

Queue Class

Represents a collection of objects that are accessed in a First-In, First-Out (FIFO) order.

Elements are added to the end of the queue and removed from the beginning.

using System.Collections;

Queue myQueue = new Queue();
myQueue.Enqueue("Task 1");
myQueue.Enqueue("Task 2");
myQueue.Enqueue("Task 3");

Console.WriteLine("Dequeue: " + myQueue.Dequeue());
Console.WriteLine("Peek: " + myQueue.Peek());

Stack Class

Represents a dynamic collection of objects that can be accessed by index. The Stack class represents a last-in, first-out (LIFO) collection of objects.

Elements are added and removed from the top of the stack.

using System.Collections;

Stack myStack = new Stack();
myStack.Push("Item A");
myStack.Push("Item B");
myStack.Push("Item C");

Console.WriteLine("Pop: " + myStack.Pop());
Console.WriteLine("Peek: " + myStack.Peek());
Generics (System.Collections.Generic)

While the System.Collections namespace provides non-generic collections, modern .NET development heavily favors the generic collections in the System.Collections.Generic namespace (e.g., List<T>, Dictionary<TKey, TValue>) for type safety and performance benefits. You can find documentation for these here.