0
0
C Sharp (C#)programming~5 mins

Generic interfaces in C Sharp (C#)

Choose your learning style9 modes available
Introduction

Generic interfaces let you create flexible blueprints that work with any data type. This helps you write code that can handle many kinds of data without repeating yourself.

When you want to create a contract for classes that can work with different types of data.
When you want to write reusable code that can handle multiple data types safely.
When you want to enforce that certain methods or properties work with a specific type, but that type can vary.
When building collections or services that should work with any type of object.
When you want to avoid code duplication for similar operations on different data types.
Syntax
C Sharp (C#)
interface IMyInterface<T>
{
    void DoSomething(T item);
    T GetItem();
}

The <T> after the interface name means it is generic and uses a placeholder type T.

Classes that implement this interface must specify the actual type for T and implement the methods using that type.

Examples
This interface defines a repository that can store and find items of any type T.
C Sharp (C#)
interface IRepository<T>
{
    void Add(T item);
    T FindById(int id);
}
This class implements IRepository for strings. It stores strings in a list.
C Sharp (C#)
class StringRepository : IRepository<string>
{
    private List<string> items = new();
    public void Add(string item) => items.Add(item);
    public string FindById(int id) => items[id];
}
This interface defines a way to compare two objects of type T.
C Sharp (C#)
interface IComparer<T>
{
    int Compare(T a, T b);
}
Sample Program

This program shows a generic interface IContainer<T> and a class Container<T> that implements it. It stores items of any type. We create containers for int and string and print their items.

C Sharp (C#)
using System;
using System.Collections.Generic;

interface IContainer<T>
{
    void AddItem(T item);
    T GetItem(int index);
}

class Container<T> : IContainer<T>
{
    private List<T> items = new();

    public void AddItem(T item) => items.Add(item);

    public T GetItem(int index) => items[index];
}

class Program
{
    static void Main()
    {
        IContainer<int> intContainer = new Container<int>();
        intContainer.AddItem(10);
        intContainer.AddItem(20);

        Console.WriteLine(intContainer.GetItem(0));
        Console.WriteLine(intContainer.GetItem(1));

        IContainer<string> stringContainer = new Container<string>();
        stringContainer.AddItem("hello");
        stringContainer.AddItem("world");

        Console.WriteLine(stringContainer.GetItem(0));
        Console.WriteLine(stringContainer.GetItem(1));
    }
}
OutputSuccess
Important Notes

Generic interfaces help avoid repeating code for different types.

When implementing a generic interface, you must specify the type or keep it generic.

You can use multiple generic parameters like interface IPair.

Summary

Generic interfaces use placeholders for types to make code reusable.

They define methods or properties that work with any specified type.

Classes implement these interfaces by specifying the actual type.