Given class ABC : IX, IY { }
and Foo<ABC>
, would you expect to be able to use new XY()
? Because you should not have this expectation. The compiler will not be either.
T will not always be XY. T will be ABC, DEF, or anything else that can implement your two interfaces and therefore satisfy the limitations that you have. XY does not convert to ABC, DEF, or any of the infinite possibilities for T, and therefore you have an error message: implicit conversion of XY to T is not possible.
What would be legal is simply new T()
, and this is only true if the method is limited to support it.
void Foo<T>() where T : IX, IY, new() { T obj = new T(); }
source share