This tutorial will briefly introduce you to the term and features of C-Sharp Generics.
The word Generic means not specific.
The C# programming language allows users to define classes, methods, static methods, interfaces, abstract classes, properties, events, delegates, and operators.
The feature of C# defines classes and methods without defining the data types during the definition of the class called Generics.
Generics help in improving the code reusability, type safety and have better performance as it does not require boxing, unboxing, and typecasting of variables and objects.
Generics are easy to implement and provide cleaner code for understanding and execution.
Generics were introduced as a part of .Net Framework 2.0 and it is available under the namespace System.Collections.Generic.
A generic class can be a base class as well as a derived class.

  • Reusability — generic type definition can be used for multiple purposes;
  • Type safety — generic provides options to use multiple data types for achieving the same purpose;
  • Performance — generics provide better performance over normal system types as they reduce the need for typecasting, boxing, and unboxing;

A generic class can be a base class as well as a derived class.
The angular brackets represent that the class/method is a generic type, and the compiler will replace all the placeholders with the data type provided.
Generic classes are defined using class names followed by type parameters inside angular brackets.
You can see the defined MyGenericsClass source code:

In the above example, MyGenericsClass is the class name and T is the type parameter.
The type parameter can be used for the fields, properties, return types, and delegates inside the class.

See the generic class using multiple parameters, TKey and TValue are type parameters:

The type parameter that is being used in the class will be replaced with the data type string
Let’s instantiated the generic class with data type as a string:

This shows the code allows the properties to be accessed using the object that is instantiated.

Generic classes can also be used as a Base/Derived class:
You can see the generic derived class is assigned with the type as a string:

Generic methods can be used within a non-generic class.

I can specify the type of type parameter explicitly while calling the method:

The .NET provides several generic classes and interfaces in System.Collections.Generic namespace …

  • HashSet<T>
  • LinkedList<T>
  • List<T>
  • Queue<T>
  • Stack<T>
  • ICollection<T>
  • IComparer<T>
  • IEnumerable<T>
  • IEnumerator<T>
  • ILIst<T>

Generic Constraints are validations that we can put on the generic type parameter.

There are six types of constraints.

  1. where T: struct  – Type argument must be a value type
  2. where T: class – Type argument must be a reference type
  3. where T: new() – Type argument must have a public parameterless constructor.
  4. where T: <base class> – Type argument must inherit from <base class> class.
  5. where T: <interface> –  Type argument must implement from <interface> interface.
  6. where T: U – There are two types of arguments T and U. T must be inherited from U.

You can see one example with the usage of the above constraints in Generic methods.

Obviously, the source code is presented only to highlight the implementation method.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.