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.
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
}
}
0 Comments