Home > C# > Find an element in a list

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);
  }
}
Advertisements
  1. No comments yet.
  1. No trackbacks yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: