Why shouldn't C# (or .NET) allow us to put a static/shared method inside an interface?
.net, c#, helper, interface, software-design
Solution
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.
Problem
Why shouldn't C# (or .NET) allow us to put a static/shared method inside an interface? Seemingly duplicate from Why we can not have Shared(static) function/methods in an interface/abstract class?, but my idea is a bit different,;I just want to put a helper for my plugins(interface) Shouldn't C# at least allow this idea? ``` namespace MycComponent { public interface ITaskPlugin : ITaskInfo { string Description { get; } string MenuTree { get; } string MenuCaption { get; } void ShowTask(Form parentForm); void ShowTask(Form parentForm, Dictionary<string, object> pkColumns); ShowTaskNewDelegate ShowTaskNew { set; get; } ShowTaskOpenDelegate ShowTaskOpen { set; get; } // would not compile with this: public static Dictionary<string, ITaskPlugin> GetPlugins(string directory) { var l = new Dictionary<string, ITaskPlugin>(); foreach (string file in Directory.GetFiles(directory)) { var fileInfo = new FileInfo(file); if (fileInfo.Extension.Equals(".dll")) { Assembly asm = Assembly.LoadFile(file); foreach (Type asmType in asm.GetTypes()) { if (asmType.GetInterface("MycComponent.ITaskPlugin") != null) { var plugIn = (ITaskPlugin)Activator.CreateInstance(asmType); l.Add(plugIn.TaskName, plugIn); } } } } return l; } // GetPlugins. would not compile inside an interface } /* because of the error above, I am compelled to put the helper method in a new class. a bit overkill when the method should be closely coupled to what it is implementing */ public static class ITaskPluginHelper { public static Dictionary<string, ITaskPlugin> GetPlugins(string directory) { var l = new Dictionary<string, ITaskPlugin>(); foreach (string file in Directory.GetFiles(directory)) { var fileInfo = new FileInfo(file); if (fileInfo.Extension.Equals(".dll")) { Assembly asm = Assembly.LoadFile(file); foreach (Type asmType in asm.GetTypes()) { if (asmType.GetInterface("MycComponent.ITaskPlugin") != null) { var plugIn = (ITaskPlugin)Activator.CreateInstance(asmType); l.Add(plugIn.TaskName, plugIn); } } } } return l; } // GetPlugins } // ITaskPluginHelper } ```