C# Tutorial: Interfaces
Define contracts with interface, implement multiple interfaces, and use default interface methods.
Contracts for types
An interface defines a contract — a set of members that implementing types must provide.
C# supports multiple interface implementation (unlike classes, which only have one base). This is how C# achieves flexibility without multiple inheritance.
What you'll learn in this C# interfaces tutorial
This interactive C# tutorial has 8 hands-on exercises. Estimated time: 18 minutes.
- Define and implement an interface — An interface declares methods without implementation. Classes implement them.
- Multiple interfaces — A class can implement multiple interfaces.
- Interface as parameter type — Accepting an interface type makes functions work with any implementation.
- Default interface methods — C# 8+ allows default method implementations in interfaces.
- IComparable<T> — Implementing `IComparable<T>` allows sorting with built-in methods.
- IEnumerable<T> — Implement `IEnumerable<T>` to make your type usable in foreach.
- Interface segregation — Many small interfaces beat one large interface.
- Explicit interface implementation — Explicitly implement when two interfaces have the same method name.
C# Interfaces concepts covered
- Contracts for types