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

windows - Why does Vista not allow creation of shortcuts to "Programs" on a NonAdmin account? Not supposed to install apps from NonAdmin account? -

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

unit testing - How to mock PreferenceManager in Android? -