I have a list of IAnimal
List<IAnimal> Animals
Inside this list I have 3 different Animal
Cat
5 objectsDog
10 objectsCow
3 objectsHow can I generate 3 different lists of the sub Animal
type?
Result should be
List<Cat> Cats
contains 5 objectsList<Dog> Dogs
contains 10 objectsList<Cow> Cows
contains 3 objectsI don't mind of using different collection type then List
. IEnumerable
or any others?
LINQ makes this simple:
var cats = animals.OfType<Cat>().ToList();
var dogs = animals.OfType<Dog>().ToList();
var cows = animals.OfType<Cow>().ToList();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With