On Apr 22, 7:46*am, Emilio <emilio.dedomini...@gmail.com> wrote:
> Good evening to everyone
>
> as the subject says,
>
> can an Interface be implemented only by a particular type of class
> (for example by a "Car" class).
There are some hacks with generics, technically... for example:
interface IFoo<T> where T : Car, IFoo<T> { ... }
Now a class C can implement IFoo<C> only if it's derived from Car.
In practice, this is rather pointless, because you really get a family
of interfaces here, with distinct one for each T - so you cannot use
them for polymorphism. Furthermore, the original idea (interface
that's only implemented by a single class) has the same flaw -
interfaces enable polymorphism, and using them for one class defeats
that purpose, so why would you even want to do that?
|