IList to IList

后端 未结 8 1031
猫巷女王i
猫巷女王i 2021-01-05 00:28

I have a few classes:

class Vehicle
{
}

class Car : Vehicle
{
}

I have a list of the derived class: IList cars;

相关标签:
8条回答
  • 2021-01-05 01:00

    You can also take a look on Krzysztof's Cwalina article, Simulated Covariance for .NET Generics

    0 讨论(0)
  • 2021-01-05 01:07

    Here are a couple of approaches using Linq:

    IList<Derived> list = new List<Derived>();
    list.Add(new Derived());
    
    IList<Base> otherlist = new List<Base>(from item in list select item as Base);
    IList<Base> otherlist2 = new List<Base>(list.Select(item => item as Base));
    
    0 讨论(0)
提交回复
热议问题