Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

c#

generics

The following code gives me this error:

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

How can I indicate to the compiler that Customer indeed inherits from object? Or does it just not do inheritance with generic collection objects (sending a List<string> gets the same error).

using System.Collections.Generic;
using System.Windows;
using System.Windows.Documents;

namespace TestControl3423
{
    public partial class Window2 : Window
    {
        public Window2()
        {
            InitializeComponent();

            List<Customer> customers = Customer.GetCustomers();
            FillSmartGrid(customers);

            //List<CorporateCustomer> corporateCustomers = CorporateCustomer.GetCorporateCustomers();
            //FillSmartGrid(corporateCustomers);
        }


        public void FillSmartGrid(List<object> items)
        {
            //do reflection on items and display dynamically
        }
    }

    public class Customer
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Street { get; set; }
        public string Location { get; set; }
        public string ZipCode { get; set; }

        public static List<Customer> GetCustomers()
        {
            List<Customer> customers = new List<Customer>();
            customers.Add(new Customer { FirstName = "Jim", LastName = "Jones", ZipCode = "23434" });
            customers.Add(new Customer { FirstName = "Joe", LastName = "Adams", ZipCode = "12312" });
            customers.Add(new Customer { FirstName = "Jake", LastName = "Johnson", ZipCode = "23111" });
            customers.Add(new Customer { FirstName = "Angie", LastName = "Reckar", ZipCode = "54343" });
            customers.Add(new Customer { FirstName = "Jean", LastName = "Anderson", ZipCode = "16623" });
            return customers;
        }
    }
}
like image 428
Edward Tanguay Avatar asked Dec 02 '09 16:12

Edward Tanguay


People also ask

Can you pass List to a method which accepts List?

Any Collection can be passed as an argument to the constructor as long as its type extends the type of the ArrayList , as String extends Object . The constructor takes a Collection , but List is a subinterface of Collection , so you can just use the List<String> .

How do you pass a List of objects to a method in Java?

You also use a List<Object> you just add Strings to the Object list. So you essentially pass Object list, that's why it works. You can add any object the the object list including string. It works since the String extends Object.

How do you pass a List to a class in Java?

Logic is simple. Create a list using an java. util. ArrayList in the main() of the Sample class. Pass the same ArrayList to constructor of both Read and Write class constructor and initialize the list fields of both class.

Can List object to cast?

you can always cast any object to any type by up-casting it to Object first. in your case: (List<Customer>)(Object)list; you must be sure that at runtime the list contains nothing but Customer objects.


1 Answers

.NET does not have co-variance and contra-variance (yet).

That B derives from A doesn't imply that List<B> derives from List<A>. It doesn't. They are two totally different types.

.NET 4.0 will get limited co-variance and contra-variance.

like image 109
Mark Seemann Avatar answered Sep 17 '22 14:09

Mark Seemann