4 OOP Generics Collections
4 OOP Generics Collections
▪ Generics and collections are fundamental concepts that are used to create reusable
and type-safe code for handling groups of objects.
▪ Generics define classes, interfaces, and methods with placeholders for the data
types they operate on. <T> { }
▪ Use generic types to maximize code reuse, type safety, and performance.
▪ Generics are most frequently used with collections and the methods that operate
on them.
▪ Syntax: public class/interface className<T> { }
▪ Generic Methods: public void methodName<T> (T input) { … }
▪ Collections in C# are data structures used to store and manipulate groups of objects.
▪ These collections are provided by the .NET Framework and offer various
functionalities like adding, removing, and iterating over elements.
▪ Two Types of Collections:
▪ Non-generic collections (ArrayList, Hashtable, … )
▪ Generic collections
▪ List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T> …
▪ using System.Collections, System.Collections.Generic
▪ Non-generic collections like ArrayList and Hashtable store elements as objects, which
can lead to runtime type errors. To avoid these issues, it is generally recommended to
use generic collections over non-generic ones.
▪ A generic collection in C# is strongly typed. That means the data type of the
elements is known at compile time.
▪ This is in contrast to non-generic collections which can store any type of object,
and type information is not known until runtime.
linkedList.Count;
using System.Collections.Generic;
HashSet<int> uniqueNumbers = new HashSet<int>();
uniqueNumbers.Add(1);
uniqueNumbers.Add(2);
uniqueNumbers.Add(3);
sortedSet.Count;
// Returns the number of elements in the sorted set.
sortedSet.Contains(3);
// Returns true if the sorted set contains the element 3,
otherwise false.
sortedSet.Remove(5);
// Removes the element 5 from the sorted set.
3rd year - Electrical Engineering Generics and Collections in C# 16
Selecting a Collection Class
▪ Do you need a sequential list where the element is typically discarded after its value is
retrieved?
▪ Do you need to access the elements in a certain order, such as FIFO, LIFO, or random?
▪ If yes,
▪ consider using the Queue class or the Queue<T> generic class if you need first-in,
first-out (FIFO) behavior.
▪ Consider using the Stack class or the Stack<T> generic class if you need last-in, first-
out (LIFO) behavior.
▪ the LinkedList<T> generic class allows sequential access either from the head to the
tail, or from the tail to the head.
▪ For safe access from multiple threads, use the concurrent versions,
ConcurrentQueue<T> and ConcurrentStack<T>.
▪ If not, consider using the other collections.
• The ArrayList and the List<T> generic class offer access to their elements by the zero-
based index of the element.
• The Hashtable, SortedList, and StringDictionary classes, and the Dictionary<TKey,TValue>
and SortedDictionary<TKey,TValue> generic classes offer access to their elements by the
key of the element.
• SortedList<TKey,TValue> generic classes offer access to their elements by either the zero-
based index or the key of the element.
▪ One key and one value: Use any of the collections based on the IDictionary interface
or the IDictionary<TKey,TValue> generic interface. For an immutable option, consider
the IImmutableSet<T> or IImmutableDictionary<TKey,TValue> generic interfaces.