none
Why interface cannot have static methods in C#

    Question

  • Can anyone please help me to know why interface in C# cannot have static methods.

    I have tried like this then I got compile time error saying "The modifier static is not a valid for this item".

    public interface InterfacesEx
    {
        static string Test();
    }

    Can anyone help me to know why static is not valid in interfaces.

    Wednesday, April 26, 2017 11:30 AM

All replies

  • There are few detailed answers on this which can be helpful if you read, see :

    Why Doesn't C# Allow Static Methods to Implement an Interface?

    Why can static classes not implement interfaces?

    Hope it helps!


    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Blog | LinkedIn | Stack Overflow | Facebook
    profile for Ehsan Sajjad on Stack Exchange, a network of free, community-driven Q&A sites

    Wednesday, April 26, 2017 12:30 PM
  • There are several reasons behind this.

    An interface is a contract that a type guarantees to implement. To use the contract you'll need an instance of the type. Static members are not applied to an instance but shared across all instances. Therefore a static member on an interface doesn't make sense because you could never call it. Additionally an interface itself has no code, it's just a type definition. In order to have a static member space would need to be allocated to store the code. That isn't how interfaces work.

    Another reason is one of implementation. Imagine this code:

    interface IFoo
    {
       static void Bar ();
    }
    
    class Foo1 : IFoo { ... }
    class Foo2 : IFoo { ... }
    
    //Later in code
    void CallBar ( IFoo foo )
    {
       foo.Bar();
    }

    Which implementation should it call? If you say it should look at the runtime type of foo and call that version then you are basically saying it should call the method associated with that instance, hence it is an instance method. The compiler cannot determine which version to call without having the instance provided and therefore only instance methods would work here.

    You could argue that you should be able to call IFoo.Bar instead, which is how static methods work elsewhere. The problem here is that there can be only 1 implementation of IFoo.Bar, who will provide it? This also means that any implementation of IFoo must properly work with the static method.

    Note that you can have static methods that work with interfaces, you just cannot define them within the interface because there is no way to correctly reference them. In your specific scenario I would have gone this route (if I needed a static method).

    interface IFoo 
    {
    }
    
    public static class Foo
    {
       public static void Bar ( IFoo foo ) { ... }
    }
    
    //Later on
    void CallBar ( IFoo foo )
    {
       Foo.Bar(foo);
    }

    This makes it clear which implementation of Bar you want and, under the hood, it can still use the interface-specific methods.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Thursday, April 27, 2017 2:14 PM
    Moderator
  • Hi,

    You can understand the issue from the memory view.

    For the interface, if you don't have the instance for the interface, you cannot use it.

    But the static variable, before entering the main function, the variable can be used at any where. you don't have the instance.

    now, you put them together, The compiler will be confused,

    Best Regards,

    Hart

    If your issue has been resolved, please remember to close your thread by marking useful posts as answer that can be helpful for other person with same issue.


    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.


    • Edited by Hart Wang Tuesday, May 23, 2017 8:43 AM
    Tuesday, May 23, 2017 8:42 AM