Generics are classes, methods, or interfaces that adapt their behaviors depending the data types assigned to them. For example, we can make a generic method that can accept any kinds of data. We can make one method showing intdouble, or string values depending on the type indicated. If you don’t use generics, then you have to make multiple methods, or even overloaded methods for each of the possible types.

public void Show(int number)
{
    Console.WriteLine(number);
}

public void Show(double number)
{
    Console.WriteLine(number);
}

public void Show(string message)
{
    Console.WriteLine(message);
}

By using generics, we can use one generic method that can accept any values.

public void Show<E>(E item)
{
    Console.WriteLine(item);
}

Generic methods will be explained in the next lesson. You might wonder, why can’t we just use the object type since object can accept any values in C#. You can see that by using generics, we didn’t use any casting. You will learn about some limitations of generics.