One of my interview questions I dare to ask is “Can you do multiple inheritance in C#?”.
So far, the correct answer always was ‘No’, but with the release of C# 8 the answer became more nuanced. Although a C# class can implement multiple interfaces it can inherit from only one base class. Until C# 8, only the base class could provide code that is usable by the derived class.
With C# 8, interfaces can also provide code to their implementing classes. This allows us to share code without a common base class. Finally we can do a (kind of) multiple inheritance…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface ILogger | |
{ | |
void Log(LogLevel level, string message) => Console.WriteLine(message); | |
void Log(Exception ex) => Log(LogLevel.Error, ex.ToString()); | |
} |