有IsAssignableFrom方法返回一个布尔值,表示一种类型是否可以从另一种类型分配.
怎能不只有他们分配的测试从或到对方,但也知道了最低协变,以获得最佳类型?
考虑以下示例(C#4.0)
码
// method body of Func is irrelevant, use default() instead
Func<char[]> x = default(Func<char[]>);
Func<int[]> y = default(Func<int[]>);
Func<Array> f = default(Func<Array>);
Func<IList> g = default(Func<IList>);
g=x;
g=y;
y=x; // won't compile
x=y; // won't compile
// following two are okay; Array is the type for the covariance
f=x; // Array > char[] -> Func<Array> > Func<char[]>
f=y; // Array > int[] -> Func<Array> > Func<int[]>
// following …Run Code Online (Sandbox Code Playgroud)