Error cannot convert from 'System.Collections.Generic.List' to 'GameLibrary.Cards.Card'

后端 未结 1 1951
借酒劲吻你
借酒劲吻你 2021-01-29 14:55
   private List> GetCardMatchesInHand()
    {
        List list4;
        List> list = new List

        
1条回答
  •  死守一世寂寞
    2021-01-29 15:32

    It's trying to do the equivalent of:

    List tmp = new List();
    tmp.Add(list2);
    tmp.Add(list3);
    tmp.Add(list4);
    List list4 = tmp;
    

    Now you can't call Add with a list of the element type - which is why you're getting that error. You're also trying to use list4 when assigning a value to list4 for the first time.

    It's not really clear what you're trying to achieve, but if you want list4 to be all the elements of list2 and list3, you can use:

    List list4 = list2.Concat(list3).ToList();
    

    Or if you really wanted list4 to be a list of lists, you need to declare it that way and call the appropriate constructor:

    // Note not using list4 within the initializer...
    List> list4 = new List> { list2, list3 };
    

    Your whole metho looks a little confused at the moment - you're not using the list variable at all, and you're declaring list4 much earlier than you use it. Neither of these is an error, but both are a little odd.

    0 讨论(0)
提交回复
热议问题