Why can't Dictionary> be cast to Dictionary>?

前端 未结 3 1317
深忆病人
深忆病人 2021-01-20 21:23

I\'m wondering why I can\'t just cast (I have a vague idea this might have something to do with that co/contravariance stuff?), and am I forced to copy the elements of the f

3条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-20 22:05

    You can not cast, because it still is a Dictionary>

    Lets say

    Dictionary> d1 = new Dictionary>();
    Dictionary> d2 = (Dictionary>)d1; // this is the invalid cast
    d2["one"] = new int[0]; // valid for d2
    List list1 = d1["one"]; // would fail
    

提交回复
热议问题