none
Static method in an Interface?

    Question

  • Is there a good reason why static method cannot be defined for an interface?

     

    Wednesday, February 07, 2007 11:59 AM

Answers

  • Remember that a static member is only accessible through a type reference, not an instance reference.  Therefore in order to call a static member it must be type.member.  If you were to allow static members on an interface then you would still have to use type.member to invoke them.  In this case type would have to be a class or struct that implements it.  Therefore even though it is defined in the interface it is still only accessible through the type that implements the interface.  I don't see that you gain anything there.  The whole purpose of interfaces is to isolate yourself from the underlying implementation so if I had to do this:

    IMyInterface ifc = ...;  //new MyInterfaceImpl();
    MyInterfaceImpl.SomeStaticMember();

    Then I've just defeated the purpose of using an interface.  If I know what the underlying type is then I would just use it.  Now you could argue (and perhaps you are) that the syntax would be:

    IMyInterface ifc = ...; //new MyInterfaceImpl();
    IMyInterface.SomeStaticMember();

    Although this syntax could work I don't see that you gain anything.  The only real benefit of having a static member is so that you can expose a member without having to create an instance of the type first.  With a static interface member you'd have to create an instance of the underlying type just so you can call a static method on it.  That's exactly what an instance member is used for.  So I still see no benefit in using a static method in an interface. 

    This is all just my opinion.  Maybe some others can chime in with their opinions.  If it is any consoliation there is talk of interfaces supporting base implementations of members in a future version of .NET.  It will not be in v3 (AFAIK) but perhaps a future version. 

    Michael Taylor - 2/8/07
    http://p3net.mvps.org

    Thursday, February 08, 2007 2:13 PM

All replies

  • A static method must be implemented in the type that defines it.  An interface has no implementation.  Therefore a static method in an interface is a contradiction.  An interface represents a contract of what will be implemented.  You never create an instance of an interface.  Instead you create instances of types that implement the interface.

    Michael Taylor - 2/7/07
    http://p3net.mvps.org

     

    Wednesday, February 07, 2007 5:30 PM
  • Thanks but what you've said applies to any method or property of an interface/class. You could equally state that "a method must be implemented in the type that defines it". I understand what interfaces are..

    I don't want to implement the static method in the interface, I merely want to declare it must exist, as I would for a normal method or property.

    e.g.

    Interface ITest {
      static ITest CreateInstanceUsingSomeData( ... );

    and then in a class implementing the interface I would have to implement it.

     

    Thursday, February 08, 2007 6:59 AM
  • Remember that a static member is only accessible through a type reference, not an instance reference.  Therefore in order to call a static member it must be type.member.  If you were to allow static members on an interface then you would still have to use type.member to invoke them.  In this case type would have to be a class or struct that implements it.  Therefore even though it is defined in the interface it is still only accessible through the type that implements the interface.  I don't see that you gain anything there.  The whole purpose of interfaces is to isolate yourself from the underlying implementation so if I had to do this:

    IMyInterface ifc = ...;  //new MyInterfaceImpl();
    MyInterfaceImpl.SomeStaticMember();

    Then I've just defeated the purpose of using an interface.  If I know what the underlying type is then I would just use it.  Now you could argue (and perhaps you are) that the syntax would be:

    IMyInterface ifc = ...; //new MyInterfaceImpl();
    IMyInterface.SomeStaticMember();

    Although this syntax could work I don't see that you gain anything.  The only real benefit of having a static member is so that you can expose a member without having to create an instance of the type first.  With a static interface member you'd have to create an instance of the underlying type just so you can call a static method on it.  That's exactly what an instance member is used for.  So I still see no benefit in using a static method in an interface. 

    This is all just my opinion.  Maybe some others can chime in with their opinions.  If it is any consoliation there is talk of interfaces supporting base implementations of members in a future version of .NET.  It will not be in v3 (AFAIK) but perhaps a future version. 

    Michael Taylor - 2/8/07
    http://p3net.mvps.org

    Thursday, February 08, 2007 2:13 PM
  • On a related note,how about this-

     

    public abstract class Abst

    {

    protected static int i;

    public static void DoSomething()

    {

    i = 2;

    }

    }

     

    class Program : Abst

    {

    /// <summary>

    /// The main entry point for the application.

    /// </summary>

    [STAThread]

    static void Main()

    {

    Program.DoSomething();

    }

    public new static void DoSomething()

    {

    i = 6;

    }

    }

    Friday, February 09, 2007 9:05 AM
  • Not sure whether your comment was a question or additional feedback Karthik so I'll just make one comment for others to be aware of.  Be careful not to confuse what is going on with static members and inheritance.  Static members are not inherited under any circumstances.  So with Abst you have a static method and with Program you have another, independent static method.  Both are callable by anybody and there is never confusion about which one will be called.  If you were to leave out the implementation of DoSomething from Program then the app will not compile.

    Michael Taylor - 2/9/07
    http://p3net.mvps.org

     

    Friday, February 09, 2007 1:54 PM