What are the differences between Generics and Collections?
- .NET provides Collection types which can be used to store and work on sequence of objects or data, with built-in utility methods.
- Collections are like dynamic arrays - whose length is adjusted based on the number of elements that are being inserted into the types.
- Collections are a part of the System.Collections namespace. These can accept any type of data without a constraint on the datatype being inserted. The elements inserted are of type Object.
- Examples of Collections are IEnumerable, ICollection, ArrayList, HashTable, Stack, Queue etc.
- Generics are similar to collections, but implemented using Type parameters.
- Generic collections accept a type parameter and accept the elements of only those type for which the generic collection is instantiated. These enforce strict type checks.
- Examples of Generic collections are List, Dictionary, Generic implementations of IEnumerable, Stack, Queue, LinkedList, HashSet etc.
- The Generics are a part of the System.Collections.Generic namespace
Posted May 14, 2021