![]() |
VOOZH | about |
Default interface methods were introduced in C# 8.0 to allow interfaces to provide a default implementation for their members. This enables extending interfaces without breaking existing implementations, which was previously not possible since adding a new method to an interface required all implementing classes to define it.
Regular Interface Method must be implemented by every class that implements the interface meanwhile Default Interface Method provides a built-in implementation, classes can use it as-is or override it.
Here we define an interface IDevice with one abstract method TurnOn and one default method ShowInfo.
We create two classes Phone and Laptop that implement IDevice.
We create instances of Phone and Laptop and call their methods.
Output
Phone is turning on
This is a device.
Laptop is turning on
This is a laptop