0Pricing
C# Academy · Lesson

Generic Interfaces

Abstract over types.

Generic Interfaces is a free C# Academy lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

What Is a Generic Interface?

A generic interface declares type parameters in angle brackets after its name. Implementers choose the concrete types, and the interface contract adapts accordingly.

The .NET library uses them everywhere, for example IEnumerable<T> and IComparable<T>.

interface IRepository<T>
{
    void Add(T item);
    T Get(int id);
}

Implementing a Generic Interface

A class can implement a generic interface by supplying a concrete type argument. The method signatures then use that real type.

This produces a type-safe, reusable contract without casts.

using System.Collections.Generic;

class IntStore : IRepository<int>
{
    private readonly List<int> data = new();
    public void Add(int item) => data.Add(item);
    public int Get(int id) => data[id];
}

interface IRepository<T>
{
    void Add(T item);
    T Get(int id);
}

Staying Generic in the Implementer

A class can implement a generic interface while remaining generic itself, forwarding its own type parameter to the interface.

This keeps the implementation reusable for any element type.

using System.Collections.Generic;

class Store<T> : IRepository<T>
{
    private readonly List<T> data = new();
    public void Add(T item) => data.Add(item);
    public T Get(int id) => data[id];
}

interface IRepository<T> { void Add(T item); T Get(int id); }

IComparable<T> in Action

IComparable<T> is a built-in generic interface. Implementing it lets your type define how instances are ordered, enabling sorting.

The CompareTo method returns negative, zero, or positive.

class Money : IComparable<Money>
{
    public int Cents;
    public int CompareTo(Money? other)
        => Cents.CompareTo(other?.Cents ?? 0);
}

Implementing IEnumerable<T>

IEnumerable<T> is the heart of LINQ and foreach. A type that implements it can be iterated. Often you delegate to an inner collection's iterator.

Iterator methods with yield return make this easy.

using System.Collections;
using System.Collections.Generic;

class Bag<T> : IEnumerable<T>
{
    private readonly List<T> items = new();
    public void Add(T x) => items.Add(x);
    public IEnumerator<T> GetEnumerator() => items.GetEnumerator();
    IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

A Runnable Generic Interface

This complete program defines a generic IPrinter<T> and implements it for strings, then uses it.

The interface guarantees a Print method whatever the concrete type.

using System;

interface IPrinter<T> { void Print(T value); }

class ConsolePrinter<T> : IPrinter<T>
{
    public void Print(T value) => Console.WriteLine(value);
}

class Program
{
    static void Main()
    {
        IPrinter<string> p = new ConsolePrinter<string>();
        p.Print("hello generics");
    }
}

Covariance With out

A generic interface parameter marked out is covariant: it appears only in output positions. This lets IEnumerable<Cat> be used where IEnumerable<Animal> is expected.

Covariance preserves assignment compatibility along an inheritance chain.

interface IProducer<out T>
{
    T Produce();
}

Contravariance With in

A parameter marked in is contravariant: it appears only in input positions. This lets an IComparer<Animal> be used where an IComparer<Cat> is needed.

Contravariance flips the direction of assignability for consumers.

interface IConsumer<in T>
{
    void Consume(T item);
}

Multiple Type Parameters

Generic interfaces can take more than one parameter. IDictionary<TKey, TValue> is a familiar example with separate key and value types.

Each parameter can independently be invariant, covariant, or contravariant.

interface IMapper<TIn, TOut>
{
    TOut Map(TIn input);
}

Programming to the Interface

Depending on a generic interface rather than a concrete class makes code flexible. You can swap implementations without changing callers.

Method parameters typed as IEnumerable<T> accept lists, arrays, and query results alike.

using System.Collections.Generic;
using System.Linq;

int Total(IEnumerable<int> values) => values.Sum();

Constraints Using Generic Interfaces

Generic interfaces frequently appear as constraints. where T : IComparable<T> lets a method sort or compare its inputs.

This combines two generics features for safe, reusable algorithms.

T Min<T>(T a, T b) where T : IComparable<T>
    => a.CompareTo(b) <= 0 ? a : b;

Quick Check

Test your understanding of generic interfaces.

Recap

Generic interfaces declare type parameters and let implementers fix or forward them. Built-in examples include IEnumerable<T>, IComparable<T>, and IDictionary<TKey, TValue>.

Variance annotations out and in enable covariance and contravariance, and generic interfaces pair naturally with where constraints.

Frequently asked questions

Is the “Generic Interfaces” lesson free?

Yes — the full text of “Generic Interfaces” is free to read here on the web, and the C# Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.

What will I learn in “Generic Interfaces”?

Abstract over types. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start C# Academy?

No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Generic Interfaces” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this C# Academy lesson?

Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Generic Methods
  2. Generic Classes
  3. where Constraints
  4. Generic Interfaces
← Back to C# Academy