将列表<派生类>转换为列表<BaseClass>虽然我们可以从基类/接口继承,但为什么我们不能声明List<>使用相同的类/接口?interface A{ }class B : A{ }class C : B{ }class Test{
static void Main(string[] args)
{
A a = new C(); // OK
List<A> listOfA = new List<C>(); // compiler Error
}}有绕道吗?
3 回答

汪汪一只猫
TA贡献1898条经验 获得超8个赞
List<A> listOfA = new List<C>().ConvertAll(x => (A)x);
List<A> listOfA = new List<C>().Cast<A>().ToList();

犯罪嫌疑人X
TA贡献2080条经验 获得超4个赞
- 3 回答
- 0 关注
- 453 浏览
添加回答
举报
0/150
提交
取消