Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Code work despite the interface is not implemented

Tags:

c#

interface

I have the following code :

 interface ICalculator
 {
     int Sum ( int x,  int y);
 }

 abstract class AbsCalculator
 {
     public abstract int Sum(int x, int y);
 }

 class Calculator : AbsCalculator, ICalculator
 {
     public override int Sum(int x, int y)
     {
         Console.WriteLine ( "From overriden method" );
         return x + y;
     }
 }

The following statements works fine and the program compile despite i didn't implement the interface :

Calculator calculator = new Calculator();
Console.WriteLine ( calculator.Sum(10, 20) );

So my first question is : Why did it work ?

Then, i add the following interface implementation to Calculator class :

int ICalculator.Sum(int x, int y)
{
     Console.WriteLine("From implemented method");
     return x + y;
}

When trying again the following statement :

Calculator calculator = new Calculator();
Console.WriteLine ( calculator.Sum(10, 20) );

The method overriden from the abstract still called. So my second question is : Why did the program call the method from the abstract class and not the method from the interface?

like image 596
Brahim Boulkriat Avatar asked Nov 02 '13 07:11

Brahim Boulkriat


2 Answers

There are two ways to implement interfaces in C#, either explicit or implicit but CLR has only one type of implementation - the explicit one.

When C# compiler sees that a class must implement an interface, it looks for methods matching the signatures of required classes by that interface and instructs CLR that those are implementations, to save you code.

So, in your case, When C# compiler sees that your class implements ICalculator, it will look for a public method which matches signature of Sum(int, int) and will consider that method the implementation. The fact that that method is overriden is not important.

If you want to give another meaning to the implementation of the method, you should give it explicitly.

like image 139
bbeda Avatar answered Oct 24 '22 01:10

bbeda


No, you are impelementing it! Your first block of code is perfectly legal. The name and signature matches what the interface requires: int Sum(int x, int y).

Now, a signature like int ICalculator.Sum(int x, int y) is called explicit interface implementation, which means you can only access that method via a reference of the interface type.

interface ICalculator {
    int Sum ( int x,  int y);
}

abstract class AbsCalculator {
    public abstract int Sum(int x, int y);
}

class Calculator : AbsCalculator, ICalculator {
    public override int Sum(int x, int y) {
        Console.WriteLine ( "From overriden method" );
        return x + y;
    }

    int ICalculator.Sum(int x, int y) {
         Console.WriteLine("From explicitly implemented interface method");
         return x + y;
    }
}

class Program {
    static void Main() {
        Calculator calculator = new Calculator();
        ICalculator icalc = calculator;

        // These calls will print different messages.
        calculator.Sum(10,20);
        icalc.Sum(10,20);
    }
}
like image 43
Jonathon Reinhart Avatar answered Oct 24 '22 02:10

Jonathon Reinhart