Why shouldn't C#(or .NET) allow us to put a static/shared method inside an interface?

后端 未结 7 1879
无人及你
无人及你 2020-12-11 04:24

Why shouldn\'t C#(or .NET) allow us to put a static/shared method inside an interface?

seemingly duplicate from here. but my idea is a bit different one, I just wan

7条回答
  •  天命终不由人
    2020-12-11 05:02

    static methods are associated with the type in which they are declared and not relevant for overriding. If you were able to attach a static method to an interface, you would have to reference it via the interface itself, e.g. ITaskPlugin.GetPlugins(...)

    What you want to do is either:

    1) Put your method in an abstract base class, as interfaces are not designed to hold implementation code, or

    2) Create an extension method which applies to the interface and then you'll have access to it without having to use a base class.

提交回复
热议问题