Understanding Multiple Inheritance in C#
Understand why C# does not support multiple inheritance for classes and how interfaces solve this limitation.
What is Multiple Inheritance?
Multiple inheritance refers to a class inheriting behavior from more than one base class at the same time.
Why C# Doesn't Support It for Classes
C# does not allow a class to inherit from multiple classes directly, mainly to avoid the ambiguity that arises when two base classes define members with the same name, known as the diamond problem.
Achieving Multiple Inheritance with Interfaces
C# allows a class to implement multiple interfaces at once, which provides many of the benefits of multiple inheritance without the ambiguity issues of multiple base classes.
Best Practices
Favor composing behavior through multiple small interfaces rather than trying to force complex hierarchies, since this keeps your code more flexible and easier to maintain.
Seeing the Diamond Problem in Code
Imagine C# allowed two base classes. Both Printer and Scanner define Start(), and a Copier inherits both. When code calls copier.Start(), the compiler would have no clear rule for which version runs. C# simply refuses this design, so the following does not compile:
class Printer { public void Start() { } }
class Scanner { public void Start() { } }
class Copier : Printer, Scanner { } // error CS1721: cannot have multiple base classes
The Interface Approach, Step by Step
- Describe each capability as a small interface.
- Let the class implement every interface it needs, separated by commas.
- Write the method bodies in the class, or reuse shared logic through composition.
public interface IPrinter { void Print(string doc); }
public interface IScanner { string Scan(); }
public class Copier : IPrinter, IScanner
{
public void Print(string doc) => Console.WriteLine($"Printing {doc}");
public string Scan() => "scanned-page";
public void Copy() => Print(Scan());
}
IPrinter p = new Copier(); // the same object can be used as either type
IScanner s = new Copier();
A class can still inherit from one base class and implement many interfaces at the same time, for example class Copier : Device, IPrinter, IScanner. The base class must come first in the list.
Interfaces can also inherit from other interfaces. An IMultiFunction : IPrinter, IScanner interface bundles both contracts under one name, and C# allows several base interfaces because interfaces carry no conflicting state.
Resolving Name Clashes Explicitly
When two interfaces declare a member with the same name, explicit interface implementation lets you give each one its own body. The caller decides which version runs by the type it uses.
public interface IPrinter { void Start(); }
public interface IScanner { void Start(); }
public class Copier : IPrinter, IScanner
{
void IPrinter.Start() => Console.WriteLine("Warming printer");
void IScanner.Start() => Console.WriteLine("Calibrating scanner");
}
var c = new Copier();
((IPrinter)c).Start(); // Warming printer
((IScanner)c).Start(); // Calibrating scanner
Since C# 8, interfaces may also contain default method bodies. This lets an interface ship shared behaviour, though the class can override it. Use the feature sparingly; it is mainly meant for adding members to published interfaces without breaking existing implementers.
Composition as the Everyday Answer
When you want to reuse real code from two places, hold instances of helper classes as fields and forward calls to them. This "has-a" relationship avoids deep hierarchies and is easy to test, because each helper can be replaced with a fake.
public class Copier : IPrinter, IScanner
{
private readonly PrintEngine _print = new();
private readonly ScanEngine _scan = new();
public void Print(string doc) => _print.Run(doc);
public string Scan() => _scan.Capture();
}
Common Mistakes
- Creating one huge interface instead of several focused ones, which forces classes to implement methods they do not need.
- Using abstract classes where an interface would do, which uses up the single base-class slot.
- Expecting interface fields; interfaces can hold properties and methods but not instance fields.
- Overusing default interface methods to imitate class inheritance.
- Forgetting that explicitly implemented members are hidden on the class itself, so
copier.Start()will not compile until you cast to an interface.