Generic_and_Non_Generic_Collections_Methods
Generic_and_Non_Generic_Collections_Methods
Generic Collections
List<T>
Method Description Example
Add(T item) Adds an item to the end. list.Add(4);
AddRange(IEnumerable<T> Adds a range of items. list.AddRange(new[] { 5,
) 6 });
Insert(int index, T item) Inserts an item at a specific list.Insert(1, 10);
index.
Remove(T item) Removes the first list.Remove(2);
occurrence of an item.
RemoveAt(int index) Removes an item by index. list.RemoveAt(0);
Contains(T item) Checks if an item exists. list.Contains(3);
Find(Predicate<T>) Finds an item matching a list.Find(x => x > 2);
condition.
Sort() Sorts the list. list.Sort();
Clear() Removes all items. list.Clear();
Count Gets the number of items. int count = list.Count;
Dictionary<TKey, TValue>
Method Description Example
Add(TKey, TValue) Adds a key-value pair. dict.Add(1, "One");
Remove(TKey) Removes an entry by key. dict.Remove(1);
TryGetValue(TKey, out Tries to get a value by key. dict.TryGetValue(1, out
TValue) string value);
ContainsKey(TKey) Checks if a key exists. dict.ContainsKey(1);
ContainsValue(TValue) Checks if a value exists. dict.ContainsValue("One");
Clear() Removes all entries. dict.Clear();
Count Gets the number of entries. int count = dict.Count;
Queue<T>
Method Description Example
Enqueue(T item) Adds an item to the end. queue.Enqueue("Task 1");
Dequeue() Removes and returns the queue.Dequeue();
first item.
Peek() Returns the first item queue.Peek();
without removing.
Contains(T item) Checks if an item exists. queue.Contains("Task 1");
Clear() Removes all items. queue.Clear();
Stack<T>
Method Description Example
Push(T item) Adds an item to the top. stack.Push(1);
Pop() Removes and returns the stack.Pop();
top item.
Peek() Returns the top item stack.Peek();
without removing.
Contains(T item) Checks if an item exists. stack.Contains(1);
Clear() Removes all items. stack.Clear();
HashSet<T>
Method Description Example
Add(T item) Adds an item. set.Add(1);
Remove(T item) Removes an item. set.Remove(1);
Contains(T item) Checks if an item exists. set.Contains(1);
UnionWith(IEnumerable<T Combines with another set.UnionWith(new[] { 3,
>) collection. 4 });
Clear() Removes all items. set.Clear();
Non-Generic Collections
ArrayList
Method Description Example
Add(object item) Adds an item. list.Add(1);
Remove(object item) Removes the first list.Remove(1);
occurrence of an item.
RemoveAt(int index) Removes an item by index. list.RemoveAt(0);
Contains(object item) Checks if an item exists. list.Contains(1);
Clear() Removes all items. list.Clear();
Sort() Sorts the list. list.Sort();
Hashtable
Method Description Example
Add(object key, object Adds a key-value pair. table.Add(1, "One");
value)
Remove(object key) Removes an entry by key. table.Remove(1);
ContainsKey(object key) Checks if a key exists. table.ContainsKey(1);
ContainsValue(object value) Checks if a value exists. table.ContainsValue("One");
Clear() Removes all entries. table.Clear();
Queue
Method Description Example
Enqueue(object item) Adds an item to the end. queue.Enqueue(1);
Dequeue() Removes and returns the queue.Dequeue();
first item.
Peek() Returns the first item queue.Peek();
without removing.
Clear() Removes all items. queue.Clear();
Stack
Method Description Example
Push(object item) Adds an item to the top. stack.Push(1);
Pop() Removes and returns the stack.Pop();
top item.
Peek() Returns the top item stack.Peek();
without removing.
Clear() Removes all items. stack.Clear();