Search code examples
c#interfaceoverloading

C# Interface overload implementation


I have an interface with overloaded methods.

interface ISide
{
    Dictionary<string, decimal> Side(string side1, decimal cost1);
    Dictionary<string, decimal> Side(string side1, decimal cost1, string side2, decimal cost2);
}

I would like to implement only one of these depending on which class is inheriting it, but I am getting a compiler error by attempting to implement only one of these methods per class.

class Entree: ISide
{
    public Dictionary<string, decimal> Side(string side1, decimal cost1, string side2, decimal cost2);
}

In this situation, do I have to use optional parameters to achieve what I'm trying to do here?


Solution

  • You should turn the concept of string side1, decimal cost1 into an object.

    public class MenuItem
    {
        public string Name {get; set;}
        public decimal Cost {get; set;}
    }
    

    Then your interface takes a list of MenuItem

    interface ISide
    {
        Dictionary<string, decimal> Side(IEnumerable<MenuItem> sides);
    }
    

    Now, this being said. It seems weird to me that Entree is an ISide. You should try to be a little more specific about what that interface means (maybe IComeWithSides or something)