How to override virtual interface in an interface
So I know a virtual method has a base implementation for interfaces, but I want to override that in a second interface that implements that interface, as fallows.
The above gives me an error, and if I get rid of override it requires a new keyword. I could just leave it to what ever inherits it, but the functionality is very straight forward, and just need it sepperated from the generic class so I can use it in a more generic way.
12 Replies
Your interfaces shouldn't provide implementation
thhaaannnkkksss....
That's the idea, yeah
Intrerfaces provide only the signatures
Classes provide the implementation
Alot of classes implement it, and it is a super simple functionality. Now outside of arguing if I should use a feature that is implemented by C#, any ideas.
Oddly common mistake, since Microsoft decided to allow for implementation in interfaces
Use an abstract class?
can't
Whyever not?
Looks like they only half implemented it as this would make comeplete sense to do if they allowed implementing virtual method in the first place.
The enviorment I use required inheritance of a class already.
Default implementation in interfaces is only really meant to make refactoring easier. The idea is that you add
in your interface, and then proceed to implement it in classes
The code still compiles, and you can update your classes progressively
For anything else, don't use default implementations
As I may eventually do it, but alot of classes implement this interface multiple times.
Then I need a reference of INeed<T> instead of just INeed. I will cast it once I know the type T is.
Retax#0813
REPL Result: Success
Result: RuntimeType
Compile: 578.629ms | Execution: 32.166ms | React with ❌ to remove this embed.
Awesome thanks! I know this might not be generally advised, but in this case the functionality is never meant to be implemented by inheriters so won't confuse people using it.
Ah interesting, so if the interface is inherited multiple times you will need to override one of the implementations, which I was wondering how it would handle. Might look down having a INeed<T, T1, ...>.
CS0695 'INeed<T, T1>' cannot implement both 'INeed<T>' and 'INeed<T1>' because they may unify for some type parameter substitutions
Okay nevermind 0.o