简体   繁体   中英

C# How to reference default interface implementation in implementer class

Consider the following interface, with a default implementation of TestMethod

public interface TestInterface
{
    public int TestMethod()
    {
        return 15;
    }
}

Calling TestMethod in the following class will cause a StackOverflowException:

public class TestClass : TestInterface
{
    public int TestMethod()
    {
        return 1 + (this as TestInterface).TestMethod();
    }
}

Now I understand why this is, but is there any way to get around it? Something like base.TestMethod() for referencing one of the class's implemented interfaces?

I know I could rename the method in TestInterface and reference it in TestClass that way, but that would cause problems for other classes that don't need to reference the default implementation.

you need to use "public override" to do what you are asking.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM