I have something like the following classes:
public class Animal
{
...
}
public class Cow : Animal
{
...
}
public class AnimalCollection : List<Animal>
{
public Animal GetFirstAnimal<T>()
{
foreach(Animal animal in this)
{
if(animal is T)
return T as Animal
}
return null;
}
}
A few questions:
Is it possible to use FirstOrDefault on the collection instead of having the GetFirstAnimal() method? What would be the syntax if you can? Also which would be the most efficient? It isn’t going to be a massive collection.
Alternatively, is there a better way of doing the same thing all together?
1 Answer