Credit to @CodeCaster for his/her great comments that prompted this answer.
The proposal states:
Note that a class does not inherit members from its interfaces; that
is not changed by this feature:
Thus, it seems reasonable (although impossible to confirm with 100% certainty until it is shipped) that:
public interface A { int Foo() => return 1; }
public interface B { int Foo() => return 2; }
public class C : A, B { }
will work fine.
Just as the proposal shows:
new C().M(); // error: class 'C' does not contain a member 'M'
then we can assume, your version:
new C().Foo();
will also not compile.
The proposal shows:
IA i = new C();
i.M();
as valid, which is equivalent to your:
A i = new C();
i.Foo();
Since i
is declared as type A
there is no reason to assume the same would not work if A
was changed to B
- there are no collisions to speak of.
The entire point of this feature is to allow interfaces to be extended in a safe way (see this video). If this only worked if you implemented one interface, that seems contrary to the objective of the feature. And given the feature appears to be implemented in a way roughly akin to explicit interface implementation (which is why we can't invoke C.Foo()
directly), I think we can reasonably assume that it will most likely allow for multiple interface implementation.
The above code will produce the compile-time error: AnyClass does not contain a member DefaultMethod.
. This seems to indicate that something akin to explicit interface implementation is used. Also see dotnetcoretutorials.com/2018/03/25/… . – FlameA.Foo()
andB.Foo()
. – GayleA
will win if the variable is of typeA
.B
will win if the variable is of typeB
. – FlameC
, will you be able to callC.Foo()
, and which implementation will that call, or will it fail to compile becauseC.Foo()
doesn't exist, or because no best overload can be matched? – AlowC.Foo
as per my earlier comment. Now, your answer raises a great question (what if there is a third interface that 'inherits' the two of them?) - which is a great question, but a slightly different one. – FlameThis seems to indicate that something akin to explicit interface implementation is used.
– Flame