C# collections
C# collections
Keivan Damirchi
Include:
● Types of all collections in C#
● Description of each item
● Use case of each
● Examples of each
● Some of the main top methods of each
● Pros and Cons of using each
Collections mentioned:
Array, ArrayList, List, LinkedList, Queue, Stack
SortedSet, SortedDictionary, SortedList, Dictionary
NameValueCollection, ConcurrentBag, ConcurrentQueue
ConcurrentStack, ConcurrentDictionary
Array
An array is a fixed-size collection of elements of the same type. Arrays
are useful when you need to work with a fixed-size collection of
elements, and you know the size of the collection in advance.
CopyTo: Copies the elements of the array to another array starting at the specified index.
IndexOf: Returns the index of the first occurrence of the specified value in the array.
Reverse: Reverses the order of the elements in the array.
Sort: Sorts the elements in the array.
Clone: Creates a new array with the same elements as the original array.
BinarySearch: Searches the array for a specified value using a binary search algorithm.
insertion/deletion of elements is
slow
Tip: Use Array when you have a fixed number of elements and you need to
access them frequently by index.
1
ArrayList
An ArrayList is a dynamically sized collection of objects of any type.
ArrayLists are useful when you need to work with a collection of objects
that can grow or shrink in size.
Tip: Use ArrayList when you have a collection of objects whose size can
change dynamically and you need to perform operations like add, remove,
and search frequently.
2
List<T>
A List<T> is a generic dynamically sized collection of objects of a
specific type. Lists are useful when you need to work with a collection of
objects that can grow or shrink in size.
Tip: Use List<T> when you have a collection of objects whose size can
change dynamically and you need to perform type-safe operations like add,
remove, and search frequently.
3
LinkedList<T>
A LinkedList<T> is a generic collection of objects of a specific type that
can be efficiently inserted or removed at any position in the list.
AddFirst(T value): Adds the specified value to the beginning of the LinkedList<T>.
AddLast(T value): Adds the specified value to the end of the LinkedList<T>.
AddBefore(LinkedListNode<T> node, T value): Adds the specified value before
the specified node in the LinkedList<T>.
AddAfter(LinkedListNode<T> node, T value): Adds the specified value after the
specified node in the LinkedList<T>.
GetEnumerator(): Returns an IEnumerator<T> for the LinkedList<T>.
Tip: Use LinkedList<T> when you have a collection of objects whose size can
change dynamically and you need to perform frequent add and remove
operations in the middle of the collection.
4
Queue<T>
The Queue<T> class is a collection of objects arranged in a first-in,
first-out (FIFO) order. It is useful when you want to process elements in
a specific order.
FIFO (First In, First Out) Fast insertion at the end and
data structure removal from the front
Tip: Use Queue<T> when you have a collection of objects that needs to
follow a FIFO (First-In-First-Out) pattern, and you need to perform
operations like Enqueue and Dequeue frequently.
5
Stack<T>
The Stack<T> class is a collection of objects arranged in a last-in,
first-out (LIFO) order. It is useful when you want to process elements in
the reverse of the order in which they were added.
Stack memory is of
Random accessing is not possible
limited size
Tip: when you have a collection of objects that needs to follow a LIFO
(Last-In-First-Out) pattern, and you need to perform operations like Push
and Pop frequently.
6
HashSet<T>
The HashSet<T> class is a collection of unique objects that do not have
a specific order. It is useful when you want to ensure that the elements
in the collection are unique.
elements that are present in both itself and the specified collection.
elements that are present in itself and not in the specified collection.
Tip: Use HashSet<T> when you have a collection of unique objects and you
need to perform operations like add, remove, and search frequently.
7
SortedSet<T>
A collection that stores unique elements in sorted order. It is
implemented as a tree-based data structure, which provides a fast search
and retrieval of elements.
Tip: Use SortedSet<T> when you have a collection of unique objects that
need to be stored in a sorted order, and you need to perform operations like
add, remove, and search frequently.
8
Dictionary<TKey, TValue>
9
SortedDictionary<TKey, TValue>
Add(TKey, TValue): This method is used to add a new key-value pair to the SortedDictionary.
Remove(TKey): This method is used to remove a key-value pair from the SortedDictionary based on the
specified key.
ContainsKey(TKey): This method is used to check whether the SortedDictionary contains a specified key or not.
ContainsValue(TValue): This method is used to check whether the SortedDictionary contains a
specified value or not.
Keys: This property returns a collection of all keys in the SortedDictionary.
Values: This property returns a collection of all values in the SortedDictionary.
10
SortedList<TKey, TValue>
Add: This method adds a new key/value pair to the SortedList<TKey, TValue>.
Remove: This method removes the key/value pair with the specified key from the
SortedList<TKey, TValue>.
Keys: This property gets a collection containing the keys in the SortedList<TKey, TValue>.
Values: This property gets a collection containing the values in the SortedList<TKey,
TValue>.
11
NameValueCollection
Tip: Stores key-value pairs of strings, used for storing configuration settings
or HTTP headers.
12
ConcurrentBag<T>
A thread-safe collection that stores elements in an unordered manner. It
provides a thread-safe bag of elements that can be added, removed, and
accessed concurrently from multiple threads without the need for
external synchronization.
Using a lock-free
Thread-safe
algorithm
No index-based
Unordered
access
13
ConcurrentQueue<T>
ConcurrentQueue<T> is a thread-safe data structure that represents a
first-in, first-out (FIFO) collection of elements of type T, where multiple
threads can access the queue simultaneously without interfering with
each other's operations.
Optimized for
Thread-safe Scalability
concurrent access
14
ConcurrentStack<T>
ConcurrentStack<T> is a thread-safe data structure in C# that
represents a last-in, first-out (LIFO) collection of elements of type T,
where multiple threads can access the stack simultaneously without
interfering with each other's operations.
Limited
No indexing
functionality
15
ConcurrentDictionary<TKey, TValue>
16
Close();
Keivan Damirchi