Consider these two abstract classes. MyClass2 extends my BaseClass and overrides a virtual method. Note it also defines TValue of MyBaseClass as IList…
public abstract class MyBaseClass<TKey, TValue>
{
public virtual IList<TValue> DoSomeStuff()
{
IList<TValue> result;
....
return restult;
}
}
public abstract class MyClass2<TKey, TValue>: MyBaseClass<TKey, IList<TValue>>
{
public override IList<TValue> DoSomeStuff()
{
IList<TValue> result;
....
return restult;
}
}
This code does not compile. The complaint is on the return type of MyClass2.DoSomeStuff. The error is “Cannot change return type when overriding method “IList<TValue> MyClass2<TKey, TValue>.DoSomeStuff()“
I’m not clear as to why this is wrong. Why wouldn’t the compile or .net consider TValue for the overriden method to be that of MyClass2?
The problem’s in your base class part:
Replace the
IList<TValue>withTValueand it will work – the method already returns anIList.