Why can't I pass List as a parameter to a method that accepts List<object>?

前端 未结 8 605
北恋
北恋 2021-01-11 13:31

The following code gives me this error:

Cannot convert from \'System.Collections.Generic.List\' to \'System.Collections.Generic.List\'.

8条回答
  •  长情又很酷
    2021-01-11 13:59

    I agree with Winston Smith's answer.

    I just wanted to point out as an alternative (although this possibly isn't the best way to handle the situation) that while List does not derive from List, Customer[] does derive from Object[].

    Therefore, it is possible to do this:

        {
            List customers = new List();
            // ...
            FillSmartGrid(customers.ToArray());
            // ...
        }
    
        public void FillSmartGrid(object[] items)
        {
        }
    

    Of course, the downside is that you're creating an array object just so you can pass it to this function.

    提交回复
    热议问题