System.Collections.Generic Namespace
Interfaces
ICollection<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 theICollection<T>.Add(T item): Adds an item to theICollection<T>.Clear(): Removes all items from theICollection<T>.Contains(T item): Determines whether an element is in theICollection<T>.CopyTo(T[] array, int arrayIndex): Copies the elements of theICollection<T>to an array, starting at the specified array index.Remove(T item): Removes the first occurrence of a specific object from theICollection<T>.
IEnumerable<T>
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>
Supports a simple iteration over a generic collection.
Current: Gets the element in the collection at the current position of the enumerator.
IList<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 theIList<T>at the specified index.RemoveAt(int index): Removes theIList<T>item at the specified index.
IDictionary<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 anICollection<TKey>containing the keys of theIDictionary<TKey, TValue>.Values: Gets anICollection<TValue>containing the values in theIDictionary<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 theIDictionary<TKey, TValue>.ContainsKey(TKey key): Determines whether theIDictionary<TKey, TValue>contains an element with the specified key.Remove(TKey key): Removes the element with the specified key from theIDictionary<TKey, TValue>.
IReadOnlyCollection<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>
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>
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 theList<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 theList<T>class that is empty and has the specified initial capacity.List(IEnumerable<T> collection): Initializes a new instance of theList<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 theList<T>.Remove(T item): Removes the first occurrence of a specific object from theList<T>.Insert(int index, T item): Inserts an item into theList<T>at the specified index.Sort(): Sorts the elements in the entireList<T>using the default comparer.
Dictionary<TKey, TValue>
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 theDictionary<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 theDictionary<TKey, TValue>class that is empty, has the default initial capacity, and uses the specifiedIEqualityComparer<TKey>.
Methods:
Add(TKey key, TValue value): Adds an element with the specified key and value to theDictionary<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 theDictionary<TKey, TValue>.
HashSet<T>
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 theHashSet<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 theHashSet<T>class that is empty and uses the specifiedIEqualityComparer<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>
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 theQueue<T>class that is empty and has the default initial capacity.Queue(int capacity): Initializes a new instance of theQueue<T>class that is empty and has the specified initial capacity.
Methods:
Enqueue(T item): Adds an object to the end of theQueue<T>.Dequeue(): Removes and returns the object at the beginning of theQueue<T>.Peek(): Returns the object at the beginning of theQueue<T>without removing it.
Stack<T>
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 theStack<T>class that is empty and has the default initial capacity.Stack(int capacity): Initializes a new instance of theStack<T>class that is empty and has the specified initial capacity.
Methods:
Push(T item): Inserts an object onto the top of theStack<T>.Pop(): Removes and returns the object at the top of theStack<T>.Peek(): Returns the object at the top of theStack<T>without removing it.
LinkedList<T>
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 theLinkedList<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 theLinkedList<T>.AddLast(T value): Adds a new node with the specified value at the end of theLinkedList<T>.Remove(T value): Removes the first occurrence of the specified value from theLinkedList<T>.
SortedDictionary<TKey, TValue>
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 theSortedDictionary<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 theSortedDictionary<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 theSortedDictionary<TKey, TValue>.Remove(TKey key): Removes the element with the specified key from theSortedDictionary<TKey, TValue>.ContainsKey(TKey key): Determines whether theSortedDictionary<TKey, TValue>contains an element with the specified key.
SortedList<TKey, TValue>
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 theSortedList<TKey, TValue>class that is empty and uses the default comparer.SortedList(IComparer<TKey> comparer): Initializes a new instance of theSortedList<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 theSortedList<TKey, TValue>.Remove(TKey key): Removes the element with the specified key from theSortedList<TKey, TValue>.IndexOfKey(TKey key): Returns the zero-based index of the specified key in theSortedList<TKey, TValue>.