System.Collections Namespace
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.
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.
GetEnumerator(): Returns anIEnumeratorobject that can be used to iterate through 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.
Count: Gets the number of elements contained in theICollection.IsSynchronized: Gets a value indicating whether access to theICollectionis synchronized (thread safe).SyncRoot: Gets an object that can be used to synchronize access to theICollection.CopyTo(Array, Int32): Copies the entireICollectionto a compatible one-dimensionalArray, starting at the specified index of the target array.
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.
IsFixedSize: Gets a value indicating whether theIListhas a fixed size.IsReadOnly: Gets a value indicating whether theIListis read-only.Item[Int32]: Gets or sets the element at the specified index.Add(Object): Adds an element to the end of theIList.Clear(): Removes all elements from theIList.Contains(Object): Determines whether an element is in theIList.IndexOf(Object): Determines the zero-based index of the first occurrence of a value in theIList.Insert(Int32, Object): Inserts an element into theIListat the specified index.Remove(Object): Removes the first occurrence of a specific object from theIList.RemoveAt(Int32): Removes the element at the specified index from theIList.
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());
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.