How to add Static methods in interface?

Asked

Viewed 925 times

8

I have my method:

 public static CloudStorageAccount GetAccount()

And in my Interface:

  public interface IAzureStorangeService
  {
    CloudStorageAccount GetAccount()
  }

But the compiler accuses that the method has not been implemented in my class

2 answers

9


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

Static methods need to be fully determined at compile time. The 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 like a normal function, it is called exactly that way as it is declared, essentially the compiler gives no special treatment to it. The only thing more that should be considered is that in his name includes the class name (you will not always see it but internally it is there);

  • the instance method hides the first parameter. You do not see it but always have a parameter called this in it. This parameter is the instance. Using your example and considering that your class is called CloudStorage it would be something like that:

    You declare in class:

      public CloudStorageAccount GetAccount()
    

    And he compiles for:

      public CloudStorageAccount GetAccount(CloudStorage this)
    

What you also don’t see in a method statement in the interface but it’s there is that every method in the interface is public and virtual. As they all are, there is no reason to declare it. Nor would it make sense if it were not so. And if the method is virtual, it must have a virtual table that will be dynamically accessed (according to the use in runtime). This table indicates which concrete type is being implemented in current use. This table is linked to the concrete type used by this. Note that concrete type refers to the real 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.

To signature of a method static then is different from an instance method. It does not match, although it has the same name, it is not the same method. This is why the error seems to indicate something other than what is actually happening, so the compiler thinks that you have not declared the method.

Reply in the OS on the subject.

C# 8 that came out in 2019 allows static methods on interfaces (if all goes well). But it still only makes sense to call directly by type and not by instance. Thinking this was always possible, it just didn’t make much sense. As in C# 8 changes the philosophy of what the interface can do, it starts to make some sense. When leaving may ask more about the specific functioning.

6

Unable to 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 type-cast the object instance 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 type-cast above.

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.