Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why an inherited interface can't be converted to its base interface in generic context?

Tags:

c#

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?

like image 669
Yazhmog Avatar asked Sep 19 '25 09:09

Yazhmog


1 Answers

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();.

like image 197
Enigmativity Avatar answered Sep 20 '25 22:09

Enigmativity