Ctrl+Shift+B

Compilations by Steve Majewski
posts - 205, comments - 67, trackbacks - 2

My Links

News


The ideas contained herein are mine and mine alone, and do not reflect the beliefs of my employer, family, friends, faith, or society in general. Reader discretion advised.


Memberships

Avanade
Solutions Developer

Xbox Live Gamer Card

Article Categories

Archives

Post Categories

Resources

Blogroll

Podcasts

Miscellaneous

Implementing List.Find in .NET 2.0

I'm putting this here more as a reminder to myself than anything else because I'm tired of looking it up every time I need to implement one of these things (of course, isn't that why people write blogs?).

The List generic class added in .NET 2.0 (System.Collections.Generic.List) is probably my most favorite addition to .NET 2.0 (well, generics in general). No longer do I have waste time creating a custom collection class just to get a strongly typed collection for my business objects. It even has the ability to do searching against the list provided you can get past Microsoft's less-than-informative help.

The List class has a Contains method, which will search the list for an instance of a given object. However, that will only indicate whether or not the list contains the same instance of the object. So having another object with the same data will yield a false negative when using the Contains method (if duplicate data is not the desired result, of course).

The Find method is more flexible because it allows the developer to define how to compare the objects. To perform a Find on a generic list, you need to create a Predicate to do the legwork. A Predicate is nothing more than a generic delegate that defines criteria and determines whether or not the criteria are met.

There are several ways to define a Predicate, but this is the method I prefer to use because I creates clean, reusable code:

public class Person
{
    public string LastName;
    public string FirstName;

    public static Predicate<Person> FindPredicate(Person person1)
    {
        return delegate(Person person2)
        {
            return person1.LastName == person2.LastName
                && person1.FirstName == person2.FirstName;
        };
    }

    public static Predicate<Person> FindByLastNamePredicate(string lastName)
    {
        return delegate(Person person)
        {
            return person.LastName == lastName;
        };
    }
}

Here I've defined two different (if not very simplistic) Predicate functions. The Predicate function creates an inline delegate that receives a Person datatype. The Find method will always pass in an object of the List's datatype, so there is no flexibility on the method signature for the delegate. However, the data passed into the Predicate method can be anything, and the variables are available in the delegate's code block. This allows for comparison logic to be as complex as needed. Keep in mind that the Find method will only return the first item in the list that matches the criteria.

To use the predicate, the code would look something like this:

// add person if not already in the list
if (myPeeps.Find(Person.FindPredicate(peepToFind)) == null)
{
    myPeeps.Add(peepToFind);
}

// find first person with a last name of Gump
Person foundPeep = myPeeps.Find(Person.FindByLastNamePredicate("Gump"));

Of course, Predicate methods could be defined anywhere. It makes sense (to me anyway) to put them on the business object as a static method, since it doesn't hurt for a business object to know how to compare itself to given criteria.

Technorati tags: , ,

Print | posted on Tuesday, April 24, 2007 10:00 PM

Feedback

No comments posted yet.

Post Comment

Title  
Name  
Email
Url
Comment   
Please add 6 and 6 and type the answer here:

Powered by: