System.Collections Namespace

This namespace contains interfaces and classes that define collections of objects. It provides fundamental types for working with collections, such as lists, dictionaries, and sets.

Interfaces

Name Description
ICollection Represents a strongly typed collection of objects.
ICollection<T> Represents a collection of objects that can be individually accessed by index.
IList Represents a non-generic collection of objects that can be individually accessed by index.
IList<T> Represents a collection of objects that can be individually accessed by index.
IEnumerable Exposes the enumerator, which supports a simple iteration over a non-generic collection of values.
IEnumerable<out T> Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
IDictionary Represents a generic collection of key/value pairs.

Classes

Name Description
ArrayList Represents a strongly typed collection of objects that can be individually accessed by index.
Comparer Provides a default comparer for objects of the specified type.
Comparer<T> Provides a default comparer for objects of the specified type.
Hashtable Represents a collection of key and value pairs that are organized based on the hash code of the key.

Common Usage Example


using System.Collections;
using System.Collections.Generic;

// Using ArrayList (non-generic)
ArrayList myList = new ArrayList();
myList.Add(10);
myList.Add("Hello");
myList.Add(3.14);

Console.WriteLine($"ArrayList contains {myList.Count} elements.");
foreach (var item in myList)
{
    Console.WriteLine($"- {item} (Type: {item.GetType().Name})");
}

Console.WriteLine("\n------------------\n");

// Using a generic List<T> (recommended)
List<string> stringList = new List<string>();
stringList.Add("Apple");
stringList.Add("Banana");
stringList.Add("Cherry");

Console.WriteLine($"List<string> contains {stringList.Count} elements.");
foreach (string fruit in stringList)
{
    Console.WriteLine($"- {fruit}");
}