c# - FirstOrDefault on collection of objects -
i have 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 t) return t animal } return null; } }
a few questions: possible use firstordefault on collection instead of having getfirstanimal() method? syntax if can? efficient? isn't going massive collection.
alternatively, there better way of doing same thing together?
var firstcow = animals.oftype<cow>().firstordefault();
Comments
Post a Comment