Reputation: 1219
Since in C#, we can only inherit from a single base class and implement multiple interfaces.
I see this normal pattern everywhere:-
DerivedClass : BaseClass, IInterface1, IInterface2
How should one decide what goes inside BaseClass and what things inside the interfaces?
Are there any guidelines for it?
Upvotes: 0
Views: 64
Reputation: 1348
I use to follow these rules:
So it can happen that you want to have a method or property in the base class AND in the interface, and sometimes you will want it to be only in the base class because it's only a utility for its children.
But like everything in design, there're no 100% correct answers, it really depends on the context and the meaning of each object.
Upvotes: 2
Reputation: 96
Implementations should go into the base class while declarations into the interfaces. There can be some exceptions e.g. if a declaration is strongly coupled with an implementation, then both can go into base class. ( with C# 8 interfaces can have default implementations, but this should not be misused in my opinion)
Upvotes: 1