paint-brush
How to Implement a Generic Interface in a Non-generic Class in C#by@ssukhpinder
3,925 reads
3,925 reads

How to Implement a Generic Interface in a Non-generic Class in C#

by Sukhpinder SinghApril 11th, 2023
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

This article explains how to create generic interfaces of type T. How to implement a generic interface in a non-generic class in C#. The implementation is divided into two cases: Single generic type interface with non- generic class and Multiple generic typeinterface with a generic class. The article is written in the C# programming language.

People Mentioned

Mention Thumbnail
featured image - How to Implement a Generic Interface in a Non-generic Class in C#
Sukhpinder Singh HackerNoon profile picture

Learning Objective

  • How to create generic interfaces of type T
  • How to implement a generic interface in a non-generic class
  • How to implement a generic interface in a generic class

Prerequisites

Please review the basic concepts of generics to understand the article better.

Getting Started

Let us understand how to create an interface of type T. A blank interface will look like this.

public interface ITest<T> { }

An interface with one member function definition

public interface ITest<T> {
   List<T> GetList();
}

The syntax for multiple generic values for an interface

public interface ITest<T, U>{
  List<T> GetList(U value);
}

After understanding the basic syntax for interfaces with single or numerous generic types. Let's see how to implement them in a class.

Implementation in a non-generic class

The implementation is divided into two cases:

Case 1: Single generic type interface with non-generic class

The below example shows if we do not define the “T” type. Please refer ITest<T> definition above.

public class Test : ITest<T>{
  public List<T> GetList(){}
}

The compiler will throw an exception that it cannot understand the reference of T.

Solution: Let’s define the type “T.”

public class Test : ITest<int>{
   public List<int> GetList(){return null;}
}

Case 2: Multiple generic type interface with non-generic class

In the case of non-generic classes, as mentioned above, we must define the genetic types. Please refer ITest<T,U> definition above.

public class Test : ITest<int, int>{
   public List<int> GetList(int value){return null;}
}

Implementation in a generic class

The implementation is again divided into two cases:

Case 1: Single generic type interface with a generic class

Please refer ITest<T> definition above.

public class Test<T> : ITest<T>{
  public List<T> GetList(){return null;}
}

Case 2: Multiple generic type interface with a generic class

Please refer ITest<T,U> definition above.

public class Test<T, U> : ITest<T, U>{
   public List<T> GetList(U value){return null;}
}

Thank you for reading, and I hope you liked the article. Please provide your feedback in the comment section.

Follow me on

C# Publication, LinkedIn, Twitter, Dev.to, Pinterest, Substack, Wix, HackerNoon.


Also published here.