C# Generics

Summary: in this tutorial, you’ll learn about C# generics and how to define a generic method that works with any type.

Introduction to the C# generics

C# generics allow you to write code that works with more than one type. By using generics, you can write code with placeholders for types and then provide the actual types when using the code.

Suppose you need to write a method that swaps the values of two integer variables. To do that, you can define a method called SwapInt() that accepts two integer parameters like this:

static void SwapInt(ref int a, ref int b)
{
    int temp = a;
        a = b;
        b = temp;
}Code language: C# (cs)

Later, you want to swap the values of two string variables. In this case, you need to define a new method that swaps the strings:

static void SwapString(ref string a, ref string b)
{
    string temp = a;
           a = b;
           b = temp;
}Code language: C# (cs)

Both SwapInt() and SwapString() methods have the same logic except for the types of the variables that they deal with.

Imagine that you need to swap values of two variables of a new type e.g., float, you need to define a new method for each new type. As the result, you’ll have lots of Swap* methods with the same logic.

To resolve this, you can create a generic method that swaps values of variables of any type like this:

static void Swap<T>(ref T a, ref T b)
{
    T temp = a;
      a = b;
      b = temp;
}Code language: C# (cs)

In this Swap() method, instead of using a specific type, we use the placeholder T for the type.

Note that the name of the placeholder T is a community convention. It means that you can use any string like A, B, TYPE, etc.

When using the Swap() method, you can specify the specific type. For example:

int x = 10, y = 20;

Swap<int>(ref x, ref y);Code language: C# (cs)

In this example, we specify the int type inside the angle brackets (<>) that follows the method name. Since the types of x and y are int, you can make the method call short like this:

int x = 10, y = 20;

Swap(ref x, ref y);Code language: C# (cs)

The following program illustrates how to use the generic Swap() method and uses it to swap values of two integers and two string variables:

class Program
{
    static void Swap<T>(ref T a, ref T b)
    {
        T temp = a;
        a = b;
        b = temp;
    }

    public static void Main(string[] args)
    {
        // swap integers
        int x = 10, y = 20;

        Console.WriteLine($"Before swapping: x={x},y={y}");
        Swap(ref x, ref y);
        Console.WriteLine($"After swapping: x={x},y={y}");
        
        // swap strings

        string s1 = "hello", s2 = "goodbye";

        Console.WriteLine($"Before swapping: s1={s1},s2={s2}");
        Swap(ref s1, ref s2);
        Console.WriteLine($"After swapping: s1={s1},s2={s2}");
    }
}Code language: C# (cs)

Output:

Before swapping: x=10,y=20
After swapping: x=20,y=10
Before swapping: s1=hello,s2=goodbye
After swapping: s1=goodbye,s2=helloCode language: plaintext (plaintext)

C# allows you to use generics with the following:

In practice, you’ll find that generics are extensively used in the collection types like List<T>, Stack<T>, Queue<T>, etc. These are called generic collection types.

Summary

  • C# generics allow you to write general-purpose code that uses the same type in multiple places without knowing that type upfront.
  • Use C# generics to write reusable, type-neutral code.
Was this tutorial helpful ?