The C# programming language has an essential feature known as generics. It enables us to create classes, methods, structures, delegates, and interfaces with a type placeholder. These placeholders are changed by the compiler with the specified type during compilation, which guarantees type safety and also improves performance.

Generics are mainly used to create general-purpose, reusable classes and methods that work with different data types without code duplication. We need to use angle brackets (<>) to define a generic class. The angle brackets are used to declare a class or method as a generic type and accept multiple type parameters.
A generic SortedList<TKey, TValue> is created by using the SortedList<TKey, TValue> class. It stores data as key-value pairs and automatically keeps the keys sorted in ascending order.
The following syntax is used to create a generic SortedList<TKey, TValue> in C#:
Or, by using a collection initializer:
SortedList<TKey, TValue> listName = new SortedList<TKey, TValue>()
{
{ key1, value1 },
{ key2, value2 },
{ key3, value3 }
};
In this syntax:
Let us take an example to illustrate the generic in C#.
Output:
The value is 100 The value is John The value is 3.14 The value is True
Explanation:
In this example, we have taken the generic method named Display(), which can accept any type of value, such as int, string, double, or bool. After that, we call the Display() method, the compiler automatically replaces T with the actual type value, and prints the result.
Generic allows the creation of flexible, reusable, and type-safe code. There are several ways to use generics in C#. Some of them are as follows:

Here, we will discuss these generics one by one.
A generic class is an important feature in C# that allows us to define type-safe data structures or classes that can run on different data types. The actual data type is specified when the class is instantiated, which provides greater flexibility and code reusability.
Let us take an example to illustrate the generic class in C#.
Output:
Integer: 5 String: Hello
Explanation:
In this example, we have taken a class named Box<T>, where T can be any data type. In the main method, we create two instances of this generic class of integer and string type. The first instance is b, which holds the integer value, and the second instance is bb, which holds the string value.
In C#, a generic method is a method that is defined with multiple type parameters, which enables it to operate with different data types in a type-safe manner without code duplication. It is useful when we want to write a method that can handle different types without affecting performance or type safety.
Let's take an example to illustrate the generic method in C#.
Output:
The values before swapping are: m = 5, n = 10 After swapping: m = 10, n = 5 Before swapping: first = TPoint, second = Tech After swapping: first = Tech, second = Tpoint
Explanation:
In this example, we have taken a class named TPoint that contains the swapping<T> method, which takes two parameters by reference using the ref keyword. The method works to swap the values using a temporary variable. In the main method, the program first swaps two integers m and n, and prints their values before and after swapping. After that, it swaps the two strings f and s, and shows the output.
In C#, a generic interface enables us to define a contract with multiple type parameters, which makes it flexible to work with various data types. It uses a type parameter, which avoids code duplication for multiple data types. It ensures type safety and code reusability.
Let's take an example to illustrate a generic interface in C#.
Output:
The square of a number is 100
Explanation:
In this example, we have taken an interface ICalc<m> that defines a method named square_root that takes a parameter of a generic type m. After that, it creates a class dCalc and implements the interface using the type double. In the square_root method, it calculates the square root of the number. In the main method, we create an object using the interface ICalc<double>, and then call the square_root method and print the output.
In C#, a generic delegate is a type of delegate that is defined with multiple type parameters, which enables it to work with any data type. If we don't want to create delegates for different data types, we can also define a generic delegate and reuse it with any type parameter.
Let us take an example to illustrate the generic delegates in C#.
Output:
The Addition of integers: 30 The Concatenation of strings: Hello, World!
Explanation:
In this example, we have taken a generic delegate Op<T> that can work with different parameters. After that, we create methods to add integers and join strings. In the main method, the delegate is used with an integer to add numbers and with a string to concatenate text.
In C#, a generic collection is a special type of collection that is used to store any data type safely. These collections are defined in the System.Collections.Generic namespace. It is generally used in several collections, such as List<T>, Dictionary<TKey, TValue>, Queue<T>, Stack<T>, and many others.
Let's take an example to illustrate the generic collection in C#.
Output:
The numbers are: 8 16 32 The ages are: Michael: 45 Bravo: 50 Johnson: 35
Explanation:
In this example, we have taken a List<int> to store integer values, and a Dictionary <string, int> to store names with their ages. After that, we take a foreach loop to print their value in a type-safe manner.
We request you to subscribe our newsletter for upcoming updates.