How to add static methods in interface?

8

I have my following method:

 public static CloudStorageAccount GetAccount()

And in my Interface :

  public interface IAzureStorangeService
  {
    CloudStorageAccount GetAccount()
  }

But the compiler accuses that the method was not implemented in my class

    
asked by anonymous 18.11.2014 / 16:31

2 answers

9

You can not. It is part of the language specification. Static methods are methods belonging to the class and not to the instance of the class (the object). An interface needs to have its methods implemented in the instance of the class. We can say that the concrete implementation in the class must inherit from the abstract declaration in the interface. And you can not use anything static to inherit anything.

Static methods need to be fully determined at compile time. Concrete implementation of the interface can be determined at runtime. There is no compatibility between these features.

Just to understand the difference between static and instance method:

  • The static method is as if it were a normal function, it is called exactly that way as it is declared, essentially the compiler does not give any special treatment to it. The only thing else that should be considered is that in his name it includes the class name (you will not always see this but internally it's there);
  • The instance method hides the first parameter. You do not see it but always have a parameter named this in it. This parameter is the instance. Using your example and considering that your class was called CloudStorage would look something like this:

    You declare in the class:

    public CloudStorageAccount GetAccount()
    

    And it compiles to:

    public CloudStorageAccount GetAccount(CloudStorage this)
    

What you also do not see in a method declaration in the interface but it is there is that every method in the interface is public and virtual. Since they are all like this, you do not have to declare it. It would not make sense otherwise. And if the method is virtual, it must have a virtual table that will be accessed dynamically (according to its use at runtime). This table indicates which concrete type is being implemented in current use. This table is bound to the concrete type used by this . Note that concrete type refers to the actual type that your class was instantiated. With a static method this table does not exist after all the method does not belong to any instance.

The signature of a static method is then different from an instance method. It does not hit, despite having the same name, it is not the same method. So the error seems to indicate something different from what is really happening, so the compiler thinks you have not declared the method.

Answer in the SO about it .

C # 8 due out in 2019 will allow static methods on interfaces (if all goes well). But it will still only make sense by calling direct by the type and not by the instance. On second thought this was always possible, just did not make much sense. As C # 8 changes the philosophy of what the interface can do, it starts making some sense. When you leave, you can ask more about the specific operation.

    
18.11.2014 / 16:36
6

You can not implement an interface method using a static method.

Interfaces exist to define a contract between the class and users of object instances. For this to happen, it is allowed to do a type-cast instance of the object for the interface type:

var @interface = (IMinhaInterface)objeto;
@interface.MetodoDaInterface(); // agora sim temos acesso à interface a partir do objeto

That is, everything related to interfaces talks about object instances, usable through the above type-cast.

You can, however, implement as a static part of the class the singleton pattern, which allows you to use the interface for something static (at least something that will be served statically). Or use a dependency injection framework, which allows you to use objects in static contexts, and serve them to your dependents.

    
18.11.2014 / 16:41