Why does a generic type constraint result in a no implicit reference conversion error?

前端 未结 5 1467
滥情空心
滥情空心 2020-12-04 10:58

I have created a couple of interfaces and generic classes for working with agenda appointments:

interface IAppointment where T : IAppointmentPropert         


        
5条回答
  •  北海茫月
    2020-12-04 11:40

    Let's simplify:

    interface IAnimal { ... }
    interface ICage where T : IAnimal { void Enclose(T animal); } 
    class Tiger : IAnimal { ... }
    class Fish : IAnimal { ... }
    class Cage  : ICage where T : IAnimal { ... }
    ICage cage = new Cage();
    

    Your question is: why is the last line illegal?

    Now that I have rewritten the code to simplify it, it should be clear. An ICage is a cage into which you can place any animal, but a Cage can only hold tigers, so this must be illegal.

    If it were not illegal then you could do this:

    cage.Enclose(new Fish());
    

    And hey, you just put a fish into a tiger cage.

    The type system does not permit that conversion because doing so would violate the rule that the capabilities of the source type must not be less than the capabilities of the target type. (This is a form of the famous "Liskov substitution principle".)

    More specifically, I would say that you are abusing generics. The fact that you've made type relationships that are too complicated for you to analyze yourself is evidence that you ought to simplify the whole thing; if you're not keeping all the type relationships straight and you wrote the thing then your users surely will not be able to keep it straight either.

提交回复
热议问题