Disabling this behavior (known as βtotal varianceβ) is necessary, because otherwise the following code will be compiled:
List<string> strlist = new List<string>(); List<object> objlist = strlist; objlist.Add(42);
We have added a number to the string list. Not good. (By the way, the code will compile for arrays instead of List , because Java allowed this for some reason, however this will result in a runtime exception.)
You can avoid this in your case:
static void Call<U, T>(T x) where U : Base where T : OtherBase<U> { }
And name it as follows:
Call(new OtherSub<Sub());
In addition, C # 4.0 provides a common dispersion for interfaces . However, their use is often not required.
source share