I would like a Covariant collection whose items can be retrieved by index. IEnumerable is the only .net collection that I'm aware of that is Covariant, but it does not have this index support.
Specifically, I'd like to do this:
List<Dog> dogs = new List<Dog>(); IEnumerable<Animal> animals = dogs; IList<Animal> animalList = dogs; // This line does not compile
Now, I'm aware of why this is a problem. List implements
ICollection that has an Add method. By up casting to
IList of Animals, it would allow subsequent code to add any type of animal which is not allowed in the "real"
So is anyone aware of a collection that supports index lookups that is also covariant? I would like to not create my own.