According to https://blogs.msdn.microsoft.com/dotnet/2018/11/12/building-c-8-0/, one of the new features coming in C# 8 is the default implementation of interfaces. Will this new feature also implicitly allow for multiple inheritance? If not, what exactly will happen if I try the following:
public interface A { int Foo() => 1; } public interface B { int Foo() => 2; } public class C : A, B { }
Syntax. iteration-statement: do statement while ( expression ) ; The expression in a do-while statement is evaluated after the body of the loop is executed. Therefore, the body of the loop is always executed at least once. The expression must have arithmetic or pointer type.
The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of the statements. The do-while loop is mainly used in the case where we need to execute the loop at least once.
Syntax. do { statement(s); } while( condition ); Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop executes once before the condition is tested. If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop executes again.
Your question is answered by Mads Torgersen in the blog post you linked to:
Actually interfaces are still quite far from abstract classes. Classes don’t inherit members from interfaces, so if a class leaves a member M implemented by the interface, the class does not have a member M! It’s like an explicit implementation today; you have to convert to the interface in order to get at such members.
So with your example:
public interface A { int Foo() => 1; } public interface B { int Foo() => 2; } public class C : A, B { }
You cannot do this:
var something = new C(); var x = something.Foo(); /* does not compile */
You can do the following:
var something = new C(); var x = ((A)something).Foo(); /* calls the implementation provided by A */ var y = ((B)something).Foo(); /* calls the implementation provided by B */
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With