问题
So in C# 8 interfaces will be able to have actual implementations of methods. At that point, what will be the difference between abstract classes and interfaces and why would I ever use abstract class anymore?What would be the advantage of this change in this version?
回答1:
C# 8.0 introduces a new feature called Default implementations in Interfaces and this changes many things.
Interfaces can now have the default implementation of methods. Interfaces can now have Private members. Interfaces can now have static members, this is used for parameterization of the default implementation. Interfaces can now have protected members which are not accessible by the derived class but can be accessible with a derived interface. If a class wants to implement the protected member, it has to be done by implementing the interface explicitly. Interfaces can also have virtual members, but the class can’t override the method but an interface can.
We can think that Interfaces and abstract are somewhat same now, But an interface cannot have Instance state, instance fields, instance auto-properties, cannot define class level fields or variables whereas an abstract class can have state.
There are some reasons stated for this change: Extending APIs, Interoperability with Android, iOS and supporting the traits language feature.
回答2:
The fundamental feature of interfaces is still that they cannot contain fields. If you need data, you need a base class. If not an interface will probably suffice.
回答3:
A class can extend several interfaces but only one abstract class. A interface can not have a constructor and destructor but an abstract class can.
来源:https://stackoverflow.com/questions/60844869/the-difference-between-abstract-classes-and-interfaces-in-c-sharp-8-release