When an interface method has no parameters, why is the implementation of the method with all optional parameters not recognized?

I played with additional parameters and was executed according to the following scenario.

If I have a method in my class where all parameters are optional, I can write the following code:

public class Test
{
    public int A(int foo = 7, int bar = 6)
    {
        return foo*bar;
    }
}
public class TestRunner
{
    public void B()
    {
        Test test = new Test();
        Console.WriteLine(test.A()); // this recognises I can call A() with no parameters
    }
}

If I then create an interface, for example:

public interface IAInterface
    {
        int A();
    }

If I create a Test class that implements this interface, then it will not compile, because it says that the A () interface element from IAInterface is not implemented. Why is the implementation of the interface not allowed as a method with all the optional parameters?

+5
source share
3 answers

. , . - . B :

public void B()
{
    Test test = new Test();
    Console.WriteLine(test.A(7, 6));
}

, IL.

+4

As soon as you need Test to implement IAInterface, you now have a class that does not conform to the contract. The interface must be executed explicitly. The compiler will not determine that A () and A (int foo = 7, int bar = 6) are the same because they are not. They have two excellent signatures, one of which does not allow the use of any parameters, and the other by default, if no values ​​are specified.

0
source

All Articles