Use and combine multiple interfaces
As a C# developer, in larger projects you commonly implement multiple interfaces within a single class. This knowledge is essential for creating flexible and reusable code in C#.
Execute the implementation of multiple interfaces
A class can implement multiple interfaces, allowing it to include behavior from multiple sources. This capability is important in C# because the language doesn't support multiple inheritance of classes. By implementing multiple interfaces, you can achieve the effect of multiple inheritance and provide a common API supported by various types, including value types.
To implement multiple interfaces in a C# class, each interface member must be implemented with the same name and signature as specified in the interface.
public interface ICanDrive
{
void Drive();
}
public interface ICanFly
{
void Fly();
}
public class SuperCar : ICanDrive, ICanFly
{
public void Drive()
{
// Implementation of Drive
}
public void Fly()
{
// Implementation of Fly
}
}
Multiple interfaces in a single class
Combining multiple interfaces in a single C# class involves implementing all required methods and properties from each interface. This approach allows you to mix and match capabilities and create classes that support various functionalities.
For example, consider a music player application with different types of controls. Each control must support APIs to play, pause, and report the current state. Some controls might support other features like skipping to the next track or adjusting the volume.
public interface IControl
{
void Play();
void Pause();
bool IsPlaying { get; }
}
public interface ISkipControl : IControl
{
void SkipToNext();
}
public interface IVolumeControl : IControl
{
void AdjustVolume(int level);
}
public class MusicPlayer : ISkipControl, IVolumeControl
{
public void Play() { /* Implementation */ }
public void Pause() { /* Implementation */ }
public bool IsPlaying { get; private set; }
public void SkipToNext() { /* Implementation */ }
public void AdjustVolume(int level) { /* Implementation */ }
}
In this example, the MusicPlayer class combines the functionalities of IControl, ISkipControl, and IVolumeControl interfaces, providing implementations for all required methods and properties.
In C#, default interface methods aid in combining different features. Default interface methods are inherently virtual, so they can be overridden if necessary. Class creators can select the interfaces to use, ensuring that the method from the class is always chosen. By apply multiple interfaces, you can design classes that are modular, flexible, and capable of supporting diverse functionalities. While this approach introduces some complexity in implementation, it promotes better separation of concerns and makes your codebase more resilient to changes. Ultimately, combining interfaces leads to more maintainable and scalable solutions, especially in larger projects where adaptability is key.