DEV Community

Cover image for Generic Interfaces - .Net
Sukhpinder Singh
Sukhpinder Singh

Posted on • Originally published at Medium

3 2

Generic Interfaces - .Net

C# compiler allows using interfaces generic types.

Learning Objective

  • How to create generic interfaces of type T

  • How to implements a generic interface in a non-generic class

  • How to implements a generic interface in a generic class

Prerequisites

Please go through the basic concepts of generics for a better understanding of the article.
What is Generics in C#
Multiple Generic Constraints .Net

Getting Started

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

    public interface ITest<T> { }
Enter fullscreen mode Exit fullscreen mode

An interface with one member function definition

    public interface ITest<T> {
       List<T> GetList();
    }
Enter fullscreen mode Exit fullscreen mode

The syntax for multiple generic values for an interface

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

After understanding the basic syntax for interface 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 definition above.

    public class Test : ITest<T>{
      public List<T> GetList(){}
    }
Enter fullscreen mode Exit fullscreen mode

The compiler will throw an exception that it's not able to understand the reference of T.

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

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

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 definition above.

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

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 definition above.

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

Case 2: Multiple generic type interface with a generic class

Please refer ITest definition above.

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

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.

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

AI Agent image

How to Build an AI Agent with Semantic Kernel (and More!)

Join Developer Advocate Luce Carter for a hands-on tutorial on building an AI-powered dinner recommendation agent. Discover how to integrate Microsoft Semantic Kernel, MongoDB Atlas, C#, and OpenAI for ingredient checks and smart restaurant suggestions.

Watch the video 📺

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay