Posts Tagged ‘Locate list element’

Find an element in a list

Recently I worked on one C# project. Got to say it’s hell lot easier to code :-). Found something cool while working on template based List class.

Guess that you got a list of employees stored in List, so how will you locate a particular employee by his name or based on certain employee attributes: like list all employees having salary greater than a particular employee. So our employee class will look like…

class Employee
  public bool IsSalaryGt(Object Emp) { return ((Emp as Employee).m_Sal > m_Sal); }
  private int m_Sal; // Init to some val

class Main
  private List<Employee> m_Emps;

  //So to locate all employees having salary greater than me we will  write
  void ListAllEmps_EarningMoreThanMe(Employee Me)
    List< Employee > Emps = m_Emps.FindAll(Me.IsSalaryGt);
%d bloggers like this: