The following code gives me this error:
Cannot convert from \'System.Collections.Generic.List\' to \'System.Collections.Generic.List\'.
Instead of passing List which does not work for the reasons above, could you not simply pass just an object reference then get the list type afterwards, kinda like...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace ConsoleApplication1 {
class Customer {
public int id;
public string name;
}
class Monkey {
public void AcceptsObject(object list) {
if (list is List<Customer>) {
List<Customer> customerlist = list as List<Customer>;
foreach (Customer c in customerlist) {
Console.WriteLine(c.name);
}
}
}
}
class Program {
static void Main(string[] args) {
Monkey monkey = new Monkey();
List<Customer> customers = new List<Customer> { new Customer() { id = 1, name = "Fred" } };
monkey.AcceptsObject(customers);
Console.ReadLine();
}
}
}
.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.
It's because a list of a class is not convertible to a list of the base class. It's a deliberate decision to make the language safe. This question gets asked often.
Here is my standard answer of how to work around the issue:
List<A> listOfA = new List<C>().ConvertAll(x => (A)x);
or:
List<A> listOfA = new List<C>().Cast<A>().ToList();
Also here is really good explanation from Eric Lippert himself, one of the chief architects on the C# team.
C# (at present) does not support variance for generic types.
However, if you're using C# 3.0, you can do this:
FillSmartGrid( customers.Cast<object>() );
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<Customer>
does not derive from List<Object>
, Customer[] does derive from Object[].
Therefore, it is possible to do this:
{
List<Customer> customers = new List<Customer>();
// ...
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.
Why can't the parameter be of type IList?
public void FillSmartGrid(IList items)