Subscribe to Subscribers
Talk to Webmaster Tony John

Resources » Code Snippets » C# Syntax

Using Delegate with Generic List to find an Item(s)


Posted Date:     Category: C# Syntax    
Author: Member Level: Gold    Points: 15



 


Hi,
IN this code snippet, you will be learning how to use the anonymous method with the help of delegates. In my case i'm taking a simple example. I have a class Employee as given below.

public class Employee
{
public Employee()
{

}
public Employee(int number, string name)
{
this._employeeNo = number;
this._employeeName = name;
}
int _employeeNo;
string _employeeName;

public int EmployeeNo
{
get { return _employeeNo; }
set { _employeeNo = value; }
}

public string EmployeeName
{
get { return _employeeName; }
set { _employeeName = value; }
}
}

I'm createing a collection of Employee object and i will add it to the Generic List as give below.


List employees = new List();
employees.Add(new Employee(1, "Venkatarajan"));
employees.Add(new Employee(2, "Siva"));
employees.Add(new Employee(3, "Yogananthan"));
employees.Add(new Employee(4, "Senthilkumaran"));
employees.Add(new Employee(5, "Gopinath"));

Now will look how the delegate can be used to find a Employee object in this collection.
My Scenario is, i need to find a Employee object whose Employee Number is 1. Then your code should look like

// Get the Employee detail whose Employee Number is 1
Employee employee = employees.Find(delegate(Employee emp) { return (emp.EmployeeNo == 1); });
Console.Write(employee.EmployeeName);
Console.Write(employee.EmployeeNo);

If you want to find more than one employee based on the condition, then you can use the FIndAll method and you can implement the delegate.
Then your code should look like give below.

// Get all the Employee Details whose Employee Number is greater than 1
List allEmployees = employees.FindAll(delegate(Employee emp) { return (emp.EmployeeNo > 1); });
foreach (Employee emp in allEmployees)
{
Console.WriteLine(emp.EmployeeName + " - " + emp.EmployeeNo);
}

Feel free to post your queries here.

Regards,
Brainstorming Guy aka Venkatarajan A





Did you like this resource? Share it with your friends and show your love!


Responses to "Using Delegate with Generic List to find an Item(s)"
Author: Bunty    29 Jun 2008Member Level: Gold   Points : 2
Hi,

Excellent code on delegates.

Easy to understand also.

Keep posting such valuable code.

This code will be useful to everyone .

Thanks and Regards
S.S.Bajoria



Author: Onur    16 Oct 2010Member Level: Bronze   Points : 1
hello,
i tried the code in my project and had this error:

Error 1 'System.Collections.ArrayList' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)

any help would be appreciated...



Author: Onur    16 Oct 2010Member Level: Bronze   Points : 1
hello,
i tried the code in my project and had this error:

Error 1 'System.Collections.ArrayList' does not contain a definition for 'Find' and no extension method 'Find' accepting a first argument of type 'System.Collections.ArrayList' could be found (are you missing a using directive or an assembly reference?)

any help would be appreciated...



Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Next Resource: Format currency according to a specific culture using NumberFormatInfo class
    Previous Resource: Forget Password recovery by mail
    Return to Resources
    Post New Resource
    Category: C# Syntax


    Post resources and earn money!
     
    More Resources
    Popular Tags   Tag posting guidelines   Search Tags  
    (No tags found.)
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India
    2005 - 2012 All Rights Reserved.
    .NET and other trademarks mentioned in this site belong to Microsoft and other respective trademark owners.
    Articles, tutorials and all other content offered here is for educational purpose only.
    We are not associated with Microsoft or its partners.