The difference between abstract classes and interfaces in C# 8 release? [closed]

℡╲_俬逩灬. 提交于 2020-07-31 03:27:36

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!