---
title: "How to implement a generic interface in C#? Provide an example."  
description: "How to implement a generic interface in C#? Provide an example."  
author: "Steilla Mitchel"  
published: 2023-11-03  
updated: 2023-11-05  
canonical: https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example  
category: "c#"  
tags: ["c#", "generics", "interface", "generic class"]  
reading_time: 2 minutes  

---

# How to implement a generic interface in C#? Provide an example.

How to implement a generic [interface in C#](https://www.mindstick.com/forum/161272/how-do-you-implement-an-interface-in-c-sharp)? Provide an example.

## Replies

### Reply by Aryan Kumar

To implement a generic [interface](https://www.mindstick.com/articles/12101/interfaces-in-java-extending-interfaces) in C#, follow these steps and see an example:

Step 1: Define the generic interface.

```plaintext
public interface IGenericInterface<T>
{
    void PrintData(T data);
}
```

In this example, **IGenericInterface** is a generic interface with one method, **PrintData**, that can work with a generic type **T**.

Step 2: Implement the generic interface in a class.

```plaintext
public class GenericClass<T> : IGenericInterface<T>
{
    public void PrintData(T data)
    {
        Console.WriteLine($"Data: {data}");
    }
}
```

Here, **GenericClass** is a class that implements the **IGenericInterface** using the same generic type **T**. It provides an implementation for the **PrintData** method.

Step 3: Use the generic class.

```plaintext
class Program
{
    static void Main()
    {
        IGenericInterface<int> intPrinter = new GenericClass<int>();
        intPrinter.PrintData(42);

        IGenericInterface<string> stringPrinter = new GenericClass<string>();
        stringPrinter.PrintData("Hello, World!");
    }
}
```

In this example, we create instances of the **GenericClass** for both **int** and **string** types and use them to print data. The interface allows us to work with different data types in a generic manner.

This demonstrates how to implement a generic interface in C#. You can use the same interface with different data types, providing flexibility and reusability in your code.


---

Original Source: https://www.mindstick.com/forum/160400/how-to-implement-a-generic-interface-in-c-sharp-provide-an-example

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
