12
interface IXXX
{
    void Foo();
}

class XXX : IXXX
{
    public static void Foo()
    {
        Console.WriteLine("From XXX");
    }
}


class Program 
{
    static void Main(string[] args)
    {
        XXX.Foo();

    }
}

Compiler error: XXX.Foo() cannot implement an interface member because it is static.

Why can't a static method implement an interface method?

Second Person Shooter
  • 14,188
  • 21
  • 90
  • 165
  • It does not make sense. How would you implement the interface in a derived class of XXX? There is no reason you cant call the static member from the implementation however. – leppie Oct 18 '10 at 08:07
  • 1
    http://stackoverflow.com/questions/259026/why-doesnt-c-allow-static-methods-to-implement-an-interface – bernhof Oct 18 '10 at 08:08
  • @leppie, you **could** have a "type method"; where there wasn't an implicit `this` but it did resolve the method at runtime from the type. The rare occasions that would use them can be catered-for by either instance methods or reflection though, so not a pressing need IMO. – Jon Hanna Oct 18 '10 at 08:12
  • @Jon Hanna: You could, but the compiler would complain not being able to resolve the correct method. – leppie Oct 18 '10 at 08:16
  • @leppie. With reflection technique it couldn't, with use of instance method it could. Does the need press enough to require a language change? I'd say no IMO though I have sometimes wanted it myself. – Jon Hanna Oct 18 '10 at 08:32

6 Answers6

13

See this thread from JoelOnSoftware describing the reasons behind this.

Basically the interface is the contract between the consumer and the provider, and a static method belongs to the class, and not each instance of the class as such.

An earlier question on SO also deal with the exact same question: Why Doesn't C# Allow Static Methods to Implement an Interface?

Community
  • 1
  • 1
Øyvind Bråthen
  • 59,338
  • 27
  • 124
  • 151
5

An interface defines the behaviour that an object must respond to. As Foo is a static method, the object doesn't respond to it. In other words, you couldn't write...

XXX myXXX = new XXX();
myXXX.Foo();

In other words, myXXX doesn't fully satisfy the requirements of the interface.

Richard Fawcett
  • 2,799
  • 1
  • 29
  • 36
3

IF we look at interfaces as a promise that an object can perform the methods listed in the interface, then ths idea of static implementation becomes problematic. If the implemetion is static, then you can't write new ImplementingObject().ImplementedMthod. The object can't perform the method, the class can.

Neowizard
  • 2,981
  • 1
  • 21
  • 39
3

You use interface to avoid using concrete class during instantiation. You can't access static method through instantiated class, so implementing interface methods with static methods is not allowed.

Tomek
  • 3,267
  • 2
  • 22
  • 23
1

Well, I believe it should allowed in case of generic type parameter. It probably simplified contractual singleton class. Here is an example:

public interface IEntity {
   // some constrains...
  DataRow ObjToRow(object obj);
  object RowToObj(DataRow dr);
}

//T would be any class inherites from IEntity with default contructor signature.
public interface IMyContract {
  T read<T>() where T : IEntity;  
  void write<T>(T object) where T : IEntity;
}

//everything in the class is static
public static class SqlProvider : IMyContract {

  public static T read<T>() where T: IEntity {
    DataRow dr = [reading from database]
    return T.RowToObj(dr);
  }

  //compile error here....
  public static void write<T>(T obj) where T : IEntity {
    DataRow dr = T.ObjToRow(obj);

   [ ... commit data row dr to database ... ]

  }
}

public static class MyAppleEntity : IEntity  {
  [... implement IEntity contract normally ... ]
}

public static class MyOrangeEntity : IEntity {
   [... implement IEntity contract normally ... ]
}

public class MyTest {
  void reading() {
     MyAppleEntity apple = SqlProvider.Read<MyAppleEntity>();
     MyOrangeEntity orange = SqlProvider.Read<MyOrangeEntity>();

     SqlProvider.write<MyAppleEntity>(apple); 
     SqlProvider.write<MyOrangeEntity>(orange);
  } 

}

The only time a type reference implicitly is in the SqlProvider.read() and write() and T is well identity at point of invoke. Without static implementation of interface I'm forced to write like this.

public class MyAppleEntity : IEntity  {
  [... implement IEntity contract normally ... ]
}

  .....

  public T read<T>() where T: IEntity, new() {
    DataRow dr = [reading from database]
    return new T().RowToObj(dr);
  }

Very little different but not quite as elegant.

Leng
  • 1,979
  • 1
  • 13
  • 7
0

Because interface member are public and overridable, and that static method cannot by design be overrided or abstract, Interfaces are here to define an accessible contract that must be implemented by their concrete implementation (with as many steps of abstract implementations & inherited interfaces between) and as far as I know there is no way to create an abstract static method.

dvhh
  • 4,724
  • 27
  • 33