System.Collections.Generic Namespace

Provides interfaces and classes that define semi-generic collections of objects, such as lists, dictionaries, hash sets, queues, stacks, and linked lists. Generic collections allow programmers to create collections that store specific types of objects and that prevent a programmer from adding incorrect types, which improves type safety and can reduce the need for casting.

Interfaces

ICollection<T>

interface ICollection<T> : IEnumerable<T>

Represents a strongly typed collection of objects that can be accessed by index. The generic collection is the base interface for the generic collection hierarchy.

  • Count: Gets the number of elements contained in the ICollection<T>.
  • Add(T item): Adds an item to the ICollection<T>.
  • Clear(): Removes all items from the ICollection<T>.
  • Contains(T item): Determines whether an element is in the ICollection<T>.
  • CopyTo(T[] array, int arrayIndex): Copies the elements of the ICollection<T> to an array, starting at the specified array index.
  • Remove(T item): Removes the first occurrence of a specific object from the ICollection<T>.

IEnumerable<T>

interface IEnumerable<T> : IEnumerable

Exposes an enumerator, which supports a simple iteration over a collection of a specified type.

  • GetEnumerator(): Returns an enumerator that iterates through the collection.

IEnumerator<T>

interface IEnumerator<out T> : IEnumerator, IDisposable

Supports a simple iteration over a generic collection.

  • Current: Gets the element in the collection at the current position of the enumerator.

IList<T>

interface IList<T> : ICollection<T>, IEnumerable<T>

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

  • this[int index]: Gets or sets the element at the specified index.
  • Insert(int index, T item): Inserts an item into the IList<T> at the specified index.
  • RemoveAt(int index): Removes the IList<T> item at the specified index.

IDictionary<TKey, TValue>

interface IDictionary<TKey, TValue> : ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>

Represents a collection of key/value pairs that are ordered by key. It is useful for scenarios where you need to associate keys with values and efficiently retrieve values by their key.

  • Keys: Gets an ICollection<TKey> containing the keys of the IDictionary<TKey, TValue>.
  • Values: Gets an ICollection<TValue> containing the values in the IDictionary<TKey, TValue>.
  • this[TKey key]: Gets or sets the value associated with the specified key.
  • Add(TKey key, TValue value): Adds an element with the specified key and value to the IDictionary<TKey, TValue>.
  • ContainsKey(TKey key): Determines whether the IDictionary<TKey, TValue> contains an element with the specified key.
  • Remove(TKey key): Removes the element with the specified key from the IDictionary<TKey, TValue>.

IReadOnlyCollection<T>

interface IReadOnlyCollection<out T> : IEnumerable<T>

Represents a collection that has a count and can be enumerated, but cannot be modified.

  • Count: Gets the number of elements in the collection.

IReadOnlyList<T>

interface IReadOnlyList<out T> : ICollection<T>, IEnumerable<T>, IReadOnlyCollection<T>

Represents a read-only collection of elements that can be accessed by index. Provides methods for inspecting elements but not modifying the collection.

  • this[int index]: Gets the element at the specified index in the read-only list.

Classes

List<T>

class List<T> : IList<T>, ICollection<T>, IEnumerable<T>, IList, ICollection, IEnumerable

Represents a generic collection of objects that can be individually accessed by an index and has a dynamic size. Implements the IList<T> interface.

Constructors:

  • List(): Initializes a new instance of the List<T> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of elements that the list will hold.
  • List(int capacity): Initializes a new instance of the List<T> class that is empty and has the specified initial capacity.
  • List(IEnumerable<T> collection): Initializes a new instance of the List<T> class that contains elements copied from the specified collection and has enough capacity to accommodate the number of elements copied.

Methods:

  • Add(T item): Adds an object to the end of the List<T>.
  • Remove(T item): Removes the first occurrence of a specific object from the List<T>.
  • Insert(int index, T item): Inserts an item into the List<T> at the specified index.
  • Sort(): Sorts the elements in the entire List<T> using the default comparer.

Dictionary<TKey, TValue>

class Dictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

Represents a collection of key and value pairs that are organized by key. It provides efficient lookups, additions, and removals of elements.

Constructors:

  • Dictionary(): Initializes a new instance of the Dictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the default equality comparer for the type of keys.
  • Dictionary(IEqualityComparer<TKey> comparer): Initializes a new instance of the Dictionary<TKey, TValue> class that is empty, has the default initial capacity, and uses the specified IEqualityComparer<TKey>.

Methods:

  • Add(TKey key, TValue value): Adds an element with the specified key and value to the Dictionary<TKey, TValue>.
  • TryGetValue(TKey key, out TValue value): Gets the value associated with the specified key.
  • Remove(TKey key): Removes the element with the specified key from the Dictionary<TKey, TValue>.

HashSet<T>

class HashSet<T> : ICollection<T>, IEnumerable<T>, ISet<T>, ICollection, IEnumerable

Represents a set of values of the same type. Sets are collections that do not contain duplicate elements and do not have a defined order.

Constructors:

  • HashSet(): Initializes a new instance of the HashSet<T> class that is empty and uses the default equality comparer for the type of the elements.
  • HashSet(IEqualityComparer<T> comparer): Initializes a new instance of the HashSet<T> class that is empty and uses the specified IEqualityComparer<T>.

Methods:

  • Add(T element): Adds the specified element to the set. Returns true if the element was added successfully; otherwise, false.
  • Remove(T element): Removes the specified element from the set. Returns true if the element was removed successfully; otherwise, false.
  • Contains(T element): Determines whether the set contains the specified element.

Queue<T>

class Queue<T> : ICollection<T>, IEnumerable<T>, IEnumerable

Represents a collection of objects that are accessed in the First-In, First-Out (FIFO) order. It's like a waiting line.

Constructors:

  • Queue(): Initializes a new instance of the Queue<T> class that is empty and has the default initial capacity.
  • Queue(int capacity): Initializes a new instance of the Queue<T> class that is empty and has the specified initial capacity.

Methods:

  • Enqueue(T item): Adds an object to the end of the Queue<T>.
  • Dequeue(): Removes and returns the object at the beginning of the Queue<T>.
  • Peek(): Returns the object at the beginning of the Queue<T> without removing it.

Stack<T>

class Stack<T> : ICollection<T>, IEnumerable<T>, IEnumerable

Represents a collection of objects that are accessed in the Last-In, First-Out (LIFO) order. It's like a stack of plates.

Constructors:

  • Stack(): Initializes a new instance of the Stack<T> class that is empty and has the default initial capacity.
  • Stack(int capacity): Initializes a new instance of the Stack<T> class that is empty and has the specified initial capacity.

Methods:

  • Push(T item): Inserts an object onto the top of the Stack<T>.
  • Pop(): Removes and returns the object at the top of the Stack<T>.
  • Peek(): Returns the object at the top of the Stack<T> without removing it.

LinkedList<T>

class LinkedList<T> : ICollection<T>, IEnumerable<T>, ICollection, IEnumerable

Represents a doubly linked list. A doubly linked list is a linear data structure in which each node contains a data element and also has links to the previous and next nodes in the sequence.

Constructors:

  • LinkedList(): Initializes a new instance of the LinkedList<T> class that is empty.

Methods:

  • AddAfter(LinkedListNode<T> node, T value): Adds a new node with the specified value after the specified node.
  • AddBefore(LinkedListNode<T> node, T value): Adds a new node with the specified value before the specified node.
  • AddFirst(T value): Adds a new node with the specified value at the beginning of the LinkedList<T>.
  • AddLast(T value): Adds a new node with the specified value at the end of the LinkedList<T>.
  • Remove(T value): Removes the first occurrence of the specified value from the LinkedList<T>.

SortedDictionary<TKey, TValue>

class SortedDictionary<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IDictionary, ICollection, IEnumerable

Represents a collection of key/value pairs that are sorted by key in ascending order. It is useful for scenarios where you need to associate keys with values and efficiently retrieve values by their key, with the added benefit of ordered access.

Constructors:

  • SortedDictionary(): Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the default comparer, and uses the default equality comparer for the type of keys.
  • SortedDictionary(IComparer<TKey> comparer): Initializes a new instance of the SortedDictionary<TKey, TValue> class that is empty, has the specified comparer, and uses the default equality comparer for the type of keys.

Methods:

  • Add(TKey key, TValue value): Adds an element with the specified key and value to the SortedDictionary<TKey, TValue>.
  • Remove(TKey key): Removes the element with the specified key from the SortedDictionary<TKey, TValue>.
  • ContainsKey(TKey key): Determines whether the SortedDictionary<TKey, TValue> contains an element with the specified key.

SortedList<TKey, TValue>

class SortedList<TKey, TValue> : IDictionary<TKey, TValue>, ICollection<KeyValuePair<TKey, TValue>>, IEnumerable<KeyValuePair<TKey, TValue>>, IList<TValue>, ICollection<TValue>, IEnumerable<TValue>, IDictionary, ICollection, IEnumerable

Represents a collection of key/value pairs that are sorted by key in ascending order. It provides efficient indexed access to values and ordered iteration.

Constructors:

  • SortedList(): Initializes a new instance of the SortedList<TKey, TValue> class that is empty and uses the default comparer.
  • SortedList(IComparer<TKey> comparer): Initializes a new instance of the SortedList<TKey, TValue> class that is empty and uses the specified comparer.

Methods:

  • Add(TKey key, TValue value): Adds an element with the specified key and value to the SortedList<TKey, TValue>.
  • Remove(TKey key): Removes the element with the specified key from the SortedList<TKey, TValue>.
  • IndexOfKey(TKey key): Returns the zero-based index of the specified key in the SortedList<TKey, TValue>.