No.
Although this conclusion is probably possible, it is not part of the language. You might be interested to suggest this to Roslyn (discover a new issue). Of course, this type of typical constraint may run into a problem in difficult cases, but, at least for simple ones, this is doable ... after all, where should the C # team put their time and effort into it?
Link Why common restrictions are not inherited , Damien_The_Unbeliever for comments .
In any case, in the code that you presented, although it is true that Out already gives you the type In , the general parameter TOuter not needed.
The following code works equally well:
public abstract class Inner { } public abstract class Outer<T> where T : Inner { } public abstract class Handler<TInner> // NOTE: TOuter removed where TInner : Inner { public abstract void SetValue(TInner value); } public class In : Inner { } public class Out : Outer<In> { } public class HandleOut : Handler<In>
So, you can use Outer<TInner> instead of TOuter if you need it. Of course, if you keep the TOuter list, it will be less restrictive, as it will allow any derived type of Outer<TInner> instead of any derived type of TOuter .
Since you did not add โnewโ to the general constraint, you are not creating objects of this type, but if this case comes, you can accept Func<Outer<TInner>> in the constructor.
source share