This may be the wrong title for what I am looking for, but I think it boils down to a class factory.
I have three classes:
class Horse : Animal
class Cow : Animal
What I want to create is a method in Animal that, pseudocoded, would work like this:
List<Horse or Cow> (Animal horseOrCow)
{
if (horseOrCow is of type Horse)
return a list of 10 Horse objects;
else
return a list of 10 Cow objects;
}
A simplification of course, but once I grasp how to do this, I should be able to figure out the rest.
(Edit: Typo fixed).
You can use the
is-operator andEnumerable.OfType+Enumerable.Take:assuming that there is a
List<Animal> allAnimalssomewhere.Edit:
Mammalmust also be anAnimaland sinceHorseandCowareMammalsthey should inherit from it.