I am curious to know why implementing my interface in an abstract base class does not satisfy the requirements in subclasses. Here is an example:
public interface IBase { } public interface IConcrete : IBase { } public interface IBaseManager<out T> where T : IBase { T Create(); IEnumerable<T> SelectAll(); } public interface IConcreteManager : IBaseManager<IConcrete> { } public abstract class Base : IBase { } public class Concrete1 : Base, IConcrete { } public abstract class BaseManager<T> : IBaseManager<T> where T : class, IBase { #region IBaseManager<T> Members public T Create() { throw new NotImplementedException(); } public IEnumerable<T> SelectAll() { throw new NotImplementedException(); } #endregion } public class ConcreteManager : BaseManager<Concrete>, IConcereteManager { //error occurs here }
This is a generated error:
'ConsoleApplication4.ConcreteManager' does not implement the interface element 'ConsoleApplication4.IBaseManager <ConsoleApplication4.IConcrete> .Create ()'.
'ConsoleApplication4.BaseManager <ConsoleApplication4.Concrete> .Create ()' cannot implement 'ConsoleApplication4.IBaseManager <ConsoleApplication4.IConcrete> .Create ()' because it does not have the corresponding return type 'ConsoleApplication4.IConcrete'. <w>
If I add these methods to the ConcreteManager class, everything will be fine and the compiler will be happy.
public new IConcrete Create() { return base.Create(); } public new IEnumerable<IConcrete> SelectAll() { return base.SelectAll(); }
If you simply return that the methods from returning the base class are sufficient, why do you need to add methods? Why can't the compiler call methods in the base class?
source share