Given:
public static void DoStuff<T>(ICollection<T> source)
{
Customer c = new Customer();
....
source.Add(c);
}
except c
is not of type <T>
.
So how do i add an item to a generic collection?
i tried having:
public static void DoStuff(ICollection<Human> source)
{
Customer c = new Customer();
....
source.Add(c);
}
but i don't use it because nobody can call DoStuff
:
ICollection<Customer> c;
DoStuff(c); <---error
because something about covariance, and .NET doesn't realize that Customer
descends from Human
:
class Customer : Human {}