Generic Class and Generic Method in C# with Examples – Programming, Pseudocode Example, C# Programming Example
C# Collection List Methods

Generic Class and Generic Method in C# with Examples

In C#, we can also have generic classes and generic methods i.e., classes and methods not made for a specific type but can be used with any general type.

We use <> brackets for this purpose. Suppose, we have defined a class or method with <T> and performed each operation on T inside the method or the class. And we passed an integer while calling it – <int>, then the T inside the class or the method will be changed to take int during the time of compilation.

Let’s look at generic classes first.

C# Generic Class Example

Let’s take an example.

Output:

In the above example, we have defined a generic class – class Generic<T>. Inside the class, we have treated T as normal data and declared a variable of type T – private T genericVariable;.

In the constructor also, we are taking a variable of type T – public Generic(T genericValue). Remember that the type of T will be decided during making the object of the class.

Generic<int> g = new Generic<int>(5); → Here, the type of T is an integer. So, T will become int inside the definition of the class.

Generic<string> g1 = new Generic<string>("CodesDope"); → In the object g1T is string. So, T will become a string inside the definition of the class.

After execution of Generic<int> g = new Generic<int>(5);Generic class would be something like:

C# Code:

Let’s take one more example.

C# Code:

Output:

In this example, we have defined our class to work on two generic types – T and U i.e., class Generic<T, U>.

While making the object, we have set T as int and U as string – Generic<int, string> g.

C# Constraint


We used a placeholder T in the above examples and this placeholder can be of any type. In C#, we can also constraint the type of placeholder using the where keyword. Suppose, we have defined a class as:

class ClassName<T> where T: class

In this case, T can only be reference type like class, string, etc. If we try with something else, we will get an error.

Here, class in where T: class means T can be a reference type. Let’s look at the table given below for another type of constraints.

ConstraintDescription
classMust be reference type
structMust be value type
new()Must have public parameterless constructor.
BaseClassNameMust be derivied from BaseClassName class.
InterfaceNameMust implement InterfaceName interface.
UMust be or derive from the argument supplied for U.

Let’s take an example.

Output:

hello.cs(16,5): error CS0452: The type ‘int’ must be a reference type in order to use it as type parameter ‘T’ in the generic type or method ‘Generic<T>’
hello.cs(16,26): error CS0452: The type ‘int’ must be a reference type in order to use it as type parameter ‘T’ in the generic type or method ‘Generic<T>’

In this example, we constrained the placeholder to take only reference type using class and we tried to make an object with an integer for the placeholder. Since int is a value type, we got errors during compiling the code.

Let’s try with a reference type.

C# Code:

Output:

In this example, we tried with a string which is a reference type and thus, the code compiled successfully.

C# Multiple Constraints

We can also have multiple constraints. Let’s take an example.

C# Code:

Output:

Inheritance With Generic Class in C#

We can derive a generic class to make subclasses of it. Let’s take and example.

In this example, we have made a subclass of a generic class and passed int during deriving it – class Derived: Generic<int>.

We can also make the derived class a generic class.

C# Generic Methods

We can also have generic methods similar to a generic class. Let’s take an example.

C# Code:

Output:

CodesDope
10

In this example, we have a generic method Display which has a placeholder T.

Display(“csharp-console-examples.com”);
Display(10);

Firstly, we passed a string and then an integer to the function Display and it worked fine.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.