相关疑难解决方法(0)

如何找到两种类型之间最佳拟合的最小协变类型?

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)

c# types covariance contravariance

21
推荐指数
1
解决办法
1201
查看次数

标签 统计

c# ×1

contravariance ×1

covariance ×1

types ×1