Generic in C#

Introduction

  1. Generics are introduced in C# 2.0.
  2. Generic allows us to design classes and methods decoupled from the data types.
  3. Generic classes are extensively used by collection classes available in the System.Collections.Generic namespace.
  4. It helps you to maximize code reuse, type safety, and performance.
  5. Generics are declared using the <> symbol.

Advantage

  1. The best way to make the method independent of the datatype is to use Generics.
  2. Generic makes our code type independent.
  3. Strongly type in nature.

Class as Generic

using System;

class Program
{
    private static void Main()
    {
        bool isEqual = Calculator<int>.AreEqual(10, 10);

        if (isEqual)
        {
            Console.WriteLine("Equal");
        }
        else
        {
            Console.WriteLine("Not Equal");
        }
    }
}

public class Calculator<T>
{
    public static bool AreEqual(T value1, T value2)
    {
        return value1.Equals(value2);
    }
}

Explanation

In the above example, we have created a class called Calculator as a generic. We have defined the <T> stand for type at the class level, also, we have created a static method AreEqual(), which takes 2 parameters & the method return type is Boolean (bool).

In the Main method, I have called the method by using the class name as we know. To call a static member, we have to call using the class name followed by the member name. Before calling the method, we are mentioning which data type the method can take & based on that. We can pass the values. Here we have mentioned <int> type, so we are passing integer values while calling the method.

If both the value matches, then as per the logic, we will get the o/p as Equal, or else we will get the o/p Not Equal.

Note. It is not required to use T as a type parameter. You can give any name to a type parameter.

It is recommended to use a more readable type parameter name as per requirement, like TSession, TKey, TValue, etc.

Learn more about the Type parameter Naming Guideline:

https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/generics/generic-type-parameters#type-parameter-naming-guidelines

Method as Generic

To make the AreEqual() method generic, we specify a type parameter using angular brackets, as shown below.

public static bool AreEqual<T> (T values1, T value2)

At the point when the client code wants to invoke this method, they need to specify the type they want the method to operate on.

If the user wants the AreEqual() method to work with an integer, they can invoke the method specifying int as the datatype using angular brackets, as shown below.

bool Equal = Calculator.AreEqual<int> (2,1);

To operate with string datatype.

bool Equal = Calculator.AreEqual<string>("A","B");

Note. In this example, we made the method generic. It is also possible to make classes, interfaces & delegates generic.

Main Method():

  private static void Main() {
    bool Equal = Calculator.AreEqual < int > (10, 10);

    if (Equal) {

      Console.WriteLine("Equal");
    } else

    {
      Console.WriteLine("Not Equal");
    }
  }
public class Calculator {
  public static bool AreEqual < T > (T value1, T value2) {
    return value1.Equals(value2);
  }
}

Explanation

In the above example, we have implemented a method as generic. We have created a class called Calculator and also created the method AreEqual(), which takes 2 parameters & the method return type is Boolean (bool). Here we have defined the <T> stand for type at the method level.

In the Main method, I have called the method by using the class name. As we know to call a static member, we have to call using the class name followed by the member name. Before calling the method, we mention which data type the method can take & based on that, we can pass the values. Here we have mentioned <int> type, so we are passing integer values while calling the method.

If both the value matches, then as per the logic, we will get the o/p as Equal, or else we will get the o/p Not Equal.

Summary

In this article, I have explained what is generic and what are the advantages of the generic, along with examples of Generic as a class and generic as a method.


Similar Articles