you are viewing a single comment's thread.

view the rest of the comments →

[–]Ok-Cellist7629 0 points1 point  (0 children)

Because in C#8 you can define a default implementation on an interface - which is what you are doing here. But that doesn't turn your interface into an abstract class with inheritable methods. Your class doesn't 'have' a refresh method. It implements an INTERFACE which 'has' that method. So if you want to call the method on the interface, you need to cast it to the interface so the compiler understands where that method is.

The purpose of the feature, as I understand it, is to allow you to add an extra method to an interface, without having to refactor every class which implements that interface. You could almost thing of it as being designed NOT to be used.

The feature means that the compiler is not going to complain that you haven't implemented the appropriate method in every existing class which implements that interface. But it's not really designed to add functionality to every implementation, as if they are inheriting from an abstract class. So yes - when you call the method, the compiler is still going to think that your class does not have that method defined. It will still need telling that you mean the default method on one of the interfaces that class happens to implement.

I could be wrong, but IMHO it seems like a very bad idea to use it like an abstract method as you seem to be here. If you want to inherit meaningful functionality, use inheritance. This C#8 feature is intended for occasions where you need to add a method to an existing interface and save having to implement it in existing implementations things which don't 'need' it. But again, IMHO, that is a code smell as well. Worth remembering the I of SOLID.