Consider the following code example:
public interface IPlayer
{
int Attack(int amount);
}
public interface IPowerPlayer: IPlayer
{
int IPlayer.Attack(int amount)
{
return amount + 50;
}
}
public interface ILimitedPlayer: IPlayer
{
new int Attack(int amount)
{
return amount + 10;
}
}
public class Player : IPowerPlayer, ILimitedPlayer
{
}
Using the code:
IPlayer player = new Player();
Console.WriteLine(player.Attack(5)); // Output 55, --> im not sure from this output. I can compile the code but not execute it!
IPowerPlayer powerPlayer = new Player();
Console.WriteLine(powerPlayer.Attack(5)); // Output 55
ILimitedPlayer limitedPlayer = new Player();
Console.WriteLine(limitedPlayer.Attack(5)); // Output 15
My problem is on the code:
Console.WriteLine(player.Attack(5)); // Output 55
The question is: The output should be 15 or 55?!
According to the .NET Team:
Decision: Made 2017-04-11: Runs I2.M, which is the unambiguously most specific override at runtime.
I'm not sure here beacuse of the keyword 'new' on the overridden interface?what should be the correct behaviour?
In case you need to compile it from source, you can download the source code from: https://github.com/alugili/Default-Interface-Methods-CSharp-8
55
– AbridgeIPowerPlayer
Attack
method is explicitly implementing theIPlayer
method, while theILimitedPlayer
Attack
method is implicitly implementing it. Or maybe it's because it shadows it with thenew
keyword. It's probably a combination of both. – Goebel