0% found this document useful (0 votes)
3 views

Generic_and_Non_Generic_Collections_Methods

The document provides an overview of methods available in both generic and non-generic collections in C#. It includes specific methods for collections such as List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T>, HashSet<T>, ArrayList, Hashtable, Queue, and Stack, along with their descriptions and examples. This serves as a reference for using various collection types in C# programming.

Uploaded by

sarfarajkhan254
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Generic_and_Non_Generic_Collections_Methods

The document provides an overview of methods available in both generic and non-generic collections in C#. It includes specific methods for collections such as List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T>, HashSet<T>, ArrayList, Hashtable, Queue, and Stack, along with their descriptions and examples. This serves as a reference for using various collection types in C# programming.

Uploaded by

sarfarajkhan254
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Methods of Generic and Non-Generic Collections in C#

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();

You might also like