Why can't List = List?

后端 未结 5 1150
予麋鹿
予麋鹿 2020-12-12 03:03

Why won\'t the following code work?

class parent {}
class kid:parent {}

List parents=new List;

It seems obvious t

5条回答
  •  Happy的楠姐
    2020-12-12 03:52

    If you know that the List contains List you can use an extension method to 'convert', (really just take the Parents that ARE of type child and return them in a list. eg something like:

      public static List Convert(this List input) {
          return input.OfType().ToList();
        }
    

    Im not sure if this helps you, but my 2 cents worth! I use it quite alot.

提交回复
热议问题