I am looking the whole day for a proper solution, bit I am fairly new to C#. If I am right, want something similar to the Java code
ArrayList<IAnimalStuff<? extends Animal>> ianimals = new ArrayList<>();
just for C#. Or another solution when I am on the wrong way.
Detailed Scenario: I have a base class (Animal) and multiple subclasses (e.g. Dog).
class Animal
{
}
class Dog : Animal
{
}
I create a common list of all animals, which contains objects of all kinds of different animals.
List<Animal> animals = new List<Animal>();
animals.add(new Dog()); // and so on
Additionally, I have an interface and a class for each special animal derived from this interface.
interface IAnimalStuff<TAnimal> where TAnimal : Animal
{
void doSomething(TAnimal animal);
}
public class DogStuff : IAnimalStuff<Dog>
{
public override void doSomething(Dog animal)
{
}
}
Now I want to manage one list with Animals and one list with AnimalStuff. When looping over all animals, I want to perform all Animalstuff in the other list which are valid for a dog. While a list of animals is no problem, I have my problems to create the other list.
List<IAnimalStuff<Animals>> ianimals = new List<IAnimalStuff<Animals>>();
Unlike in the first list, I only can add objects to this list of type
IAnimalStuff<Animals>
, but I also want to do
ianimals.add(GetDogStuff()); // add object of type IAnimalStuff<Dog>
I assumed this is valid, because Dog is a subclass of Animal. I think with the upper line of Java code this can be solved, but I did not find any solution for C#. Or am I on the wrong track?