Search code examples
c#inheritancecoding-styleprivate-methods

Inherit common functions in C#


I want to create many Service classes that use a common set of functions. All these classes will inherit ClientBase class which is provided by Microsoft. Currently I have written those common functionalities as private methods in class. What is the best way to do it to avoid code repetition and efficiency. I cannot add those common methods to base class as it cannot be modified.


Solution

  • Have your own abstract base class that inherits ClientBase<T>, call it MyClientBase<T>, move the common methods to it and make them protected so that successors could access them. Then make your service classes inherit MyClientBase<T>.