Tester Bill
Tester Bill

Reputation: 115

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.

Upvotes: 1

Views: 637

Answers (1)

Sye
Sye

Reputation: 27

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

Upvotes: -1

Related Questions