Generics are used to make reusable code classes to decrease the code redundancy, increase type safety, and performance.

Using generics, we can create collection classes. To create generic collection, System.Collections.Generic namespace should be used instead of classes such as ArrayList in the System.Collections namespace.

Generics promotes the usage of parameterized types.

  • Generic classes encapsulate operations that are not specific to a particular data type.
  • The most common use for generic classes is with collections like linked lists, hash tables, stacks, queues, trees, and so on.
  • Operations such as adding and removing items from the collection are performed in basically the same way regardless of the type of data being stored.

Example:

class BaseNodeGeneric<T> { }