How to find objects in Generics with List.Find() method

I’ve been looking for help on how to find objects in Generics with List.Find() method …. and … take a look what I have found.
In the follow example, I created a simple class:

public class Person
{
       private int _id;
       private string _name;

       public int ID {  get{ return _id;} set{ _id = value;}}
       public int Name {  get{ return _name;} set{ _name= value;}}

       public Person(int id, string name)
       {
             _id = id;
             _name = name;
       }
}

In the example, there’s a simple class with two private attributes. Now we’re going to create a typed List of this object and take advantage of the Find() method

public void CreateAndSearchList()
{
      //create and fill the collection
      List<Person> myList = new List<Person>();
      myList.Add(new Person(1, “AndreySanches”));
      myList.Add(new Person(2, “AlexandreTarifa”));
      myList.Add(new Person(3, “EmersonFacunte”));

     //find a specific object
     Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });
}

This is the fastest way to find objects using Generics.

Enjoy it !!!!

}

 

27 thoughts on “How to find objects in Generics with List.Find() method

  1. What is this syntax in VB.net?

    //find a specific object
    Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1; });

  2. Thanks for this tip! I was struggling with a way to do a Find using an outside value on an object property in the List. This is the answer.

  3. I’m sorta using something like this (except I defined a separate delegate method and passed the pointer to Find).

    The problem is, in order to get at the ID, you have to explicitly instantiate a specific object type (Person) in order to use this method; defeats the purpose uf using generics, doesn’t it! And what’s with the delegate, anyway? Give me a way to specify a key or let “Find” search without the need for a delegate (very hokey). I know it’s hard to code a generic “Find” because the generic list can take any type, but use Reflection, something; once the list is typed, “Find” should know how to handle it. There are other ways to check for reference type equality.

    The other issue with List is that “IndexOf” doesn’t work with objects (assuming all ref types) either.

    I know they improved performance (mostly) and added type safety over ArrayList, but we need a better generic list construct.

  4. Just wanted to say that this a very good example and as I am using Visual Studio 2008, the code by Israel works perfectly for me. Cheers!

  5. Hi Vivek,

    Take a look at the following C# code :

    Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == 1 && p.Name == “AndreySanches”; });

    Let me know if you have any other questions

  6. While this rapid fire code is nice, it leaves more to be desired. One, rarely would a dynamic code search for id = 1 and name as Andrey. In real situations when the name is not known at design time, how do you search for a person with a name that is known at run time? Secondly, I wish someone pointed out the code in vb.net.

  7. Hi Ranjit,

    Actually this code is just an example. In the real situation you would need to replace number “1” and string “AndreySanches” for a local variable.

    Eg.

    int id = Convert.ToInt32(txtCustomerId.Text);

    string name = txtCustomerName.Text;

    Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == id && p.Name == name; });

    Thanks for your comments

  8. Can u tell me it is work for datetime also.

    i ahve the same class above and contains one more property as
    private DateTime _joinDate;
    public DateTime JoinDate
    {
    set { _joinDate = value; }
    get { return _joinDate; }
    }

    now i want to find employee who joined recently??

    can you show me how this works?

    thanks in advance
    Kiran .

  9. Hi Kiran,

    You can use the “JoinDate” property to search the objects.

    e.g.
    Person myLocatedObject = myList.Find(delegate(Person p) {return p.ID == id && p.JoinDate == #yourdateobject#; });

    Let me know if you need anything else.

    Thanks

  10. Thank you so much! I have spent forever online trying to find a simple way to do this. This is so simple, and it actually works. Thanks again.

Leave a Reply to kiran Cancel reply

Your email address will not be published. Required fields are marked *