Generics In C# | Learn C#

Generics is a powerful feature of many programming languages, including C#. It allows you to write code that can work with multiple data types without having to write a separate version of the code for each data type. This makes your code more flexible and reusable and reduces the amount of code you have to write.

In C#, generics allow you to define a class or method that can work with any data type. This allows for greater flexibility and reusability of code. Using generics, you can create classes, interfaces, and methods that have placeholder types, which are then specified when the generic class or method is actually used.

Here is an example of a generic class called Stack in C#,

public class Stack<T>
{
    // The items array is used to store the items in the stack
    private T[] items;

    // The Push method adds an item to the top of the stack
    public void Push(T item)
    {
        // Code to add the item to the stack
    }

    // The Pop method removes and returns the top item from the stack
    public T Pop()
    {
        // Code to remove and return the top item from the stack
    }
}

In the above example, the Stack class is defined as a generic class, with a placeholder type T. This means that the class can be used with any data type, as long as the type is specified when the class is used.

The Push and Pop methods of the Stack class are defined to work with data of the type T. This means that they can be used with any data type, as long as the type is specified when the class is used.

To use this Stack class, you would specify the type of data that the stack should hold when you create a new instance of the class. For example:

Stack<int> stackOfInts = new Stack<int>();

This creates a new Stack object that can hold integers. You can then use the Push and Pop methods to add and remove items from the stack.

You can also use generics in method declarations. For example, you could create a generic method that allows you to swap the positions of two items in an array, like this:

public void Swap<T>(T[] array, int index1, int index2)
{
    T temp = array[index1];
    array[index1] = array[index2];
    array[index2] = temp;
}

This method can be used with any data type, as long as the type is specified when the method is called. For example:

int[] arrayOfInts = { 1, 2, 3, 4, 5 };
Swap<int>(arrayOfInts, 0, 1);

This would swap the values at the first and second indices of the arrayOfInts array.

Final Thoughts

The advantage of generics is that they allow you to write type-safe code, which means that the type of data that a class or method can work with is checked at compile time. This can help to prevent runtime errors, and makes your code more reliable and easier to debug.

Overall, generics is a valuable tool for writing efficient and flexible code in C# and other programming languages.

If you want to learn more about generics in C#, here is a good tutorial: Using Generics in C#

Check out more articles on C# Programming at C# Corner.