Generics vs non-generics in C#


There are two types of collections in C#: non-generic collections and generic collections.

Generics in C#

Generic collections hold elements of same datatypes.

For example −

  • List
  • Dictionary
  • Hashset

Dictionary − Dictionary is a collection of keys and values in C#. Dictionary <TKey, TValue> is included in the System.Collection.Generics namespace.

Hashset − HashSet in C# eliminates duplicate strings or elements in an array.In C#, it is an optimized set collection.

Non-Generics in C#

Non-generic collections hold elements of different datatypes.

The following are the non-generic collections: ArrayList, BitArray.

ArrayList − It represents ordered collection of an object that can be indexed individually. ArrayList is an alternative to an array. However, unlike array you can add and remove items from a list at a specified position using an index and the array resizes itself automatically.

BitArray −  It represents an array of the binary representation using the values 1 and 0. It is used when you need to store the bits but do not know the number of bits in advance.

Updated on: 21-Jun-2020

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements