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

后端 未结 7 1873
无人及你
无人及你 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:14

    The idea of an interface is to represent a contract, not implementation.

    I can't remember offhand whether IL actually does allow static methods with implementations in interfaces - I've a sneaky suspicion that it does - but that muddies the concept somewhat.

    I can see your point - it's sometimes useful to know what helper methods are available which are connected with an interface (and extension methods are particularly relevant there) but I would personally want to put those in a separate class anyway, just to keep the mental model clean.

提交回复
热议问题