I have an ISnack interface, which, when implemented by a class, must have a constructor without parameters without parameters. Mainly:
public interface ISnack<T> where T : new() { }
I use <T> where T : new() to force the use of a constructor without parameters.
I would then implement the interface like this:
public class Cutlet : ISnack<Cutlet> { }
This works, and it just ensures that the Cutlet class has a parameterless constructor.
Now I have an abstract base class of Kitchen :
public abstract class Kitchen<T> where T : ISnack { }
The requirement is that Kitchen should have a restriction where T should be ISnack . But this does not work, because there is no ISnack , but only ISnack<T> .
If I tried this
public abstract class Kitchen<T> where T : ISnack<T> { }
it will not compile ( 'T' must be a non-abstract type with a public parameterless constructor in order to use it as parameter 'T' in the generic type or method 'ISnack<T>' ), and also does not make sense in my context.
If I could force ISnack have a dimensionless constructor without being limited to a parameter of type T , then T in Kitchen<T> could easily be ISnack . How to do it?
source share