System.Collections Namespace
The System.Collections
namespace contains interfaces and classes that define various collections of objects, such as lists, queues, bit arrays, hash tables, and dictionaries.
Core Types
ArrayList
Represents an ordered collection of objects that can be indexed individually.
Hashtable
Implements a collection of key/value pairs that are organized based on the hash code of the key.
Queue
Represents a first-in, first-out (FIFO) collection of objects.
Stack
Represents a last-in, first-out (LIFO) collection of objects.
SortedList
Represents a collection of key/value pairs that are sorted by the keys and are accessible by key and index.
BitArray
Manages a compact array of bit values, which are represented as Booleans, where true indicates that the bit is on (1) and false indicates the bit is off (0).
Example
using System; using System.Collections; class Program { static void Main() { // Create an ArrayList and add items ArrayList list = new ArrayList(); list.Add("Apple"); list.Add("Banana"); list.Add("Cherry"); // Iterate over the collection foreach (var item in list) { Console.WriteLine(item); } // Use a Hashtable Hashtable table = new Hashtable(); table[1] = "One"; table[2] = "Two"; Console.WriteLine($"Key 1 = {table[1]}"); } }