I'm trying to implement an interface inheritance system in my C# project, but I can't get it to work.
Here is a simplified version:
public interface BaseInterface {}
public abstract class AbstractClass<T> where T : BaseInterface {}
public interface ChildInterface : BaseInterface {}
public class ConcreteClass : AbstractClass<ChildInterface> {}
I want to use it as follow:
AbstractClass<BaseInterface> c = new ConcreteClass();
The last line of code gives me the following error:
Cannot implicitly convert type 'ConcreteClass' to 'AbstractClass<BaseInterface>'
Why is the conversion impossible?
Let's have a play with your types and call them something different.
public interface IFruit { }
public abstract class BowlOf<Fruit> where Fruit : IFruit
{
public void Add(Fruit fruit) { }
}
public class Apple : IFruit { }
public class BowlOfApples : BowlOf<Apple> { }
Now, with that - which is pretty much just a rename of the types (but changing public interface ChildInterface : BaseInterface {}
to public class Apple : IFruit { }
) then we create the following issue.
Let's say I have public class Banana : IFruit { }
also and let's assume that the following is legal:
BowlOf<IFruit> c = new BowlOfApples();
Then I am perfectly fine to call c.Add(new Banana())
. What!?! You can't add a banana to a bowl of apples.
And that's why the compiler is complaining when you try to do AbstractClass<BaseInterface> c = new ConcreteClass();
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With