1

Suppose I have these 2 Interfaces.

public interface IMath2
{
    int GetSum(int x, int y)
    {
        return x + y;
    }
}

public interface IMath1
{
    int GetSum(int x, int y)
    {
        return x + y;
    }
}

and One class with inherit Imath1 and Imath2.

public class Math : IMath1,IMath2
{
    int x = 5, y= 6;
    int z = GetSum(x,y);
}

Here, I can't access the GetSum method from parent Interfaces. Please provide solutions.

Panagiotis Kanavos
  • 120,703
  • 13
  • 188
  • 236
Darshit Gandhi
  • 121
  • 1
  • 7
  • 6
    You need to pick one and cast e.g. `int z = ((IMath1)this).GetSum(x, y);` – Johnathan Barclay Sep 27 '22 at 07:25
  • Good arcticle with a very clear solution to your problem of having two interfaces withe the same method name: https://www.c-sharpcorner.com/article/how-to-inherit-multiple-interfaces-having-same-method-name/ – Marc Wittmann Sep 27 '22 at 08:06
  • @MarcWittmann this isn't a question about interface inheritance, it's about default interface implementations. The C# docs offer a better explanation in [How to explicitly implement members of two interfaces](https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/interfaces/how-to-explicitly-implement-members-of-two-interfaces) anyway. It's not enough to just make one implementation explicit - which one would that be and why? – Panagiotis Kanavos Sep 27 '22 at 08:34

2 Answers2

2

Here's a working example:

void Main()
{
    var m = new Math();
    m.ExampleGetSum(1, 2);
}

public interface IMath2
{
    int GetSum(int x, int y)
    {
        return x + y + 2;
    }
}

public interface IMath1
{
    int GetSum(int x, int y)
    {
        return x + y + 1;
    }
}

public class Math : IMath1, IMath2
{

    public void ExampleGetSum(int x, int y)
    {
        int z1 = (this as IMath1).GetSum(x, y);
        int z2 = (this as IMath2).GetSum(x, y);

        Console.WriteLine(z1);
        Console.WriteLine(z2);
    }
}

That give me this output:

4
5
Enigmativity
  • 113,464
  • 11
  • 89
  • 172
1

Your Math class needs some small adaptions to function like you want.

    public class Math : IMath1,IMath2
    {
        int x = 5;
        int y= 6;
        private int z;
        private bool oneOrTwo = true; //True = 1, False = 2
        
        public Math(bool oneOrTwo)
        {
            this.oneOrTwo = oneOrTwo;
            z = GetSum(x, y);
        }

        public int GetSum(int x, int y)
        {
            return oneOrTwo ? ((IMath2)this).GetSum(x, y) : ((IMath1)this).GetSum(x, y);
        }
    }

With the Boolean I decide from witch interface the GetSum should be used and I give z it's value in the constructor, otherwise the GetSum method would need to be static.

And you need to cast this to the interface, otherwise it doesn't know that it has these methods.

If you need to implement the methods in your class, then do this.

public int GetSum(int x, int y)
        {
            return oneOrTwo ? ((IMath1)this).GetSum(x, y) : ((IMath2)this).GetSum(x, y);
        }

        int IMath1.GetSum(int x, int y)
        {
            return x + y;
        }

        int IMath2.GetSum(int x, int y)
        {
            return x * y;
        }
SoulB
  • 68
  • 7