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

Popular posts from this blog

c++ - How do I get a multi line tooltip in MFC -

asp.net - In javascript how to find the height and width -

c# - DataTable to EnumerableRowCollection -