Search code examples
c#interfaceprivate-methodsinterface-implementation

Is it allowed for a class to implement an interface plus additional private method(s) in C#?


I have the following interface:

interface IExcelServices
{
    Dictionary<string, string[]> FilterFields(Dictionary<string, string[]>  excelContent);
    Dictionary<string, string[]> ParseExcelFile(string path);
}

Which is implemented by the following class:

public class ExcelServices : IExcelServices
{
    public Dictionary<string, string[]> FilterFields(Dictionary<string, 
       string[]>  excelContent)
    {
        //somecode
    }

    public Dictionary<string, string[]> ParseExcelFile(string path)
    {
        //somecode
    }

    private void ReleaseObject(object obj)
    {
        //somecode
    }
}

My code compiles without any problem but I was wondering whether adding a private method (or in general any method) which is not in the interface definition is a good OO programming practice or not.


Solution

  • Of course it is OK to add other members in a class that implements an interface. An interface is just a contract that specifies what the class must implement, but it can also add any members it needs (actually most classes have more members that the ones declared in the interface)