If default interface methods are implemented in C# 8.0 why would I ever need abstract classes? [closed]

别说谁变了你拦得住时间么 提交于 2019-12-19 02:47:11

问题


I recently ran into a list of features that are being considered for addition in the next version of C#. One of them is called "default interface methods":

https://github.com/dotnet/csharplang/blob/master/proposals/default-interface-methods.md

In short, it will allow you to define actual method implementations on the interfaces themselves meaning interfaces can now have implementations. Since this is the case, and C# classes can implement/inherit from multiple interfaces then why in the world would I ever use abstract classes?

The only thing that comes to my mind is that interfaces cannot have a constructor so maybe there is a need to run some logic in the abstract class constructor and that would justify defining an abstract class.

Is there any other scenario that anyone can think of?


回答1:


Apart from state as mentioned in comments,

Base Class

You can't inherit an interface from a Base class. Interface can only inherit an interface. You would need abstract class to derive from some other class. And since you can't inherit from class, you can't override class methods. Which you can override in abstract class.




回答2:


Usually class inheritance feels like a taxonomy (x 'is a' y), while interfaces are behaviours (x 'can do' y), so there is a subtle difference in implied meaning. Although you're right that the technical distinction is less clear cut.



来源:https://stackoverflow.com/questions/45641921/if-default-interface-methods-are-implemented-in-c-sharp-8-0-why-would-i-ever-nee

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