DotNet - Best Practices – Interfaces
Matías Creimerman - Buenos Aires, Argentina - 2020-02-18

In .NET the interfaces are used to design at some point the multiherence, since classes can only inherit up to 1 base class and implement multiple interfaces. On the other hand, it serves to support several types, including value types (because they cannot inherit from any other than ValueType).

As a good practice, you should define an interface when you need a common point that supports a set of types including the value ones. You should also use them when you need to inherit some functionality (or at least define it), if you already have a base class from which you inherit your class. Also avoid creating interfaces just to encapsulate but without design (i.e. only interfaces with name but without content, something that sometimes is used to be able to frame class types for some functionality, for example message types that can be used as parameter in some method are defined as IMessage, but IMessage is empty and is implemented by different class types to be able to be used as that parameter, there are other ways to do it, interfaces are not the right one). Also, you must have some class that implements the interface, it is the only way to validate the use of the interface. And it should have at least one method or parameter that implements the interface. If you are going to use the interface and add new things to it, it is better to create another interface than to modify it, because it could generate versioning or implementation problems. If you are not going to do anything that is recommended as good practice, then you are not needing an interface, but a class.

This content is property of Matias Creimerman
Any misuse of this material will be punishable
This work is licensed under a
Creative Commons Attribution-NonCommercial-NoDerivatives 4.0 International License
Creative Commons License