It's not that it can't convert the /contents/, but rather that it can't
convert the list itself. Just because A : B does not mean that List<A> :
List<B>.
The proper way to do this is to generalise the method; the parameter should
not be "a list of IPersons", but rather, "a list of /somethings/, which are
themselves IPersons"; you do this using the where operator:
public void SomeMethod<T>(List<T> items) where T : IPerson {
// do something
}
Note that this is type-safe; it knows that any item (e.g. items[0]) is a T,
which is itself an IPerson, so you get access to any members of IPerson;
however, .Add etc will only accept a T; because of this, if you plan to add
items to the collection, it can be useful to state "where T : IPerson,
new()"; this mandates that any T must have a public default ctor, hence you
can then (in SomeMethod) use "T newItem = new T();" to create new instances.
Hope this helps,
Marc
"Varangian" <of****@gmail.com> wrote in message
news:11*********************@j33g2000cwa.googlegro ups.com...
Hello, there
I have a problem with regards to System.Collections.Generic.List<T>
I need to pass a class with implements an interface - TestClass :
IPerson
I put this class in a List<TestClass> = new List<TestClass>();
then I pass this List<TestClass> to a function which takes an argument
List<IPerson> person
It's giving me an error, that it cannot convert from List<TestClass> to
IPerson, how is this possible?
what I'm missing and doing wrong ? anyone knows what should I do to
pass that List<TestClass> ?