C
C#2y ago
Xan.Nava

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.
public interface INeed<T> : INeed {
void /*.*/ Receive(
T value);

override virtual Type TypeNeeded() {
return typeof(T);
}
}

public interface INeed {
virtual Type TypeNeeded() {
return null;
}
}
public interface INeed<T> : INeed {
void /*.*/ Receive(
T value);

override virtual Type TypeNeeded() {
return typeof(T);
}
}

public interface INeed {
virtual Type TypeNeeded() {
return null;
}
}
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
Angius
Angius2y ago
Your interfaces shouldn't provide implementation
Xan.Nava
Xan.Nava2y ago
thhaaannnkkksss....
Angius
Angius2y ago
That's the idea, yeah Intrerfaces provide only the signatures Classes provide the implementation
Xan.Nava
Xan.Nava2y ago
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.
Angius
Angius2y ago
Oddly common mistake, since Microsoft decided to allow for implementation in interfaces Use an abstract class?
Xan.Nava
Xan.Nava2y ago
can't
Angius
Angius2y ago
Whyever not?
Xan.Nava
Xan.Nava2y ago
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.
Angius
Angius2y ago
Default implementation in interfaces is only really meant to make refactoring easier. The idea is that you add
public int Foo()
{
throw new NotImplementedException();
}
public int Foo()
{
throw new NotImplementedException();
}
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
Xan.Nava
Xan.Nava2y ago
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.
MODiX
MODiX2y ago
Retax#0813
REPL Result: Success
interface I {
Type GetT() => null;
}

interface I<T> : I {
Type I.GetT() => typeof(T);
}

class C : I<int> {}

((I<int>)new C()).GetT()
interface I {
Type GetT() => null;
}

interface I<T> : I {
Type I.GetT() => typeof(T);
}

class C : I<int> {}

((I<int>)new C()).GetT()
Result: RuntimeType
System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
System.Int32, System.Private.CoreLib, Version=7.0.0.0, Culture=neutral, PublicKeyToken=7cec85d7bea7798e
Compile: 578.629ms | Execution: 32.166ms | React with ❌ to remove this embed.
Xan.Nava
Xan.Nava2y ago
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