If I have a default interface method like this:
public interface IGreeter
{
void SayHello(string name) => System.Console.WriteLine($"Hello {name}!");
}
Can I have my concrete implementation call that default method?
public class HappyGreeter : IGreeter
{
public void SayHello(string name)
{
// what can I put here to call the default method?
System.Console.WriteLine("I hope you're doing great!!");
}
}
So that calling:
var greeter = new HappyGreeter() as IGreeter;
greeter.SayHello("Pippy");
Results in this:
// Hello Pippy!
// I hope you're doing great!!
Indeed Calling C# interface default method from implementing class shows that I can call method that my class does not implement, but as somewhat expected adding call to ((IGreeter)this).SayHello(name);
inside HappyGreeter.SaysHello
causes stack overflow.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…