Cannot convert from List to List

前端 未结 6 1313
庸人自扰
庸人自扰 2020-11-22 10:39

I\'m trying to pass A list of DerivedClass to a function that takes a list of BaseClass, but I get the error:

cannot convert from 
         


        
6条回答
  •  伪装坚强ぢ
    2020-11-22 11:26

    It is because List is in-variant, not co-variant, so you should change to IEnumerable which supports co-variant, it should work:

    IEnumerable bcl = new List();
    public void doSomething(IEnumerable bc)
    {
        // do something with bc
    }
    

    Information about co-variant in generic

提交回复
热议问题