The very flexible generic collection List<T> contains several methods that take a predicate as it’s parameter. Coupled with Anonymous Methods this provides powerfully concise code for filtering, searching and sorting your collections.
People.Add(new Person("Randy", "Patterson", 40));
People.Add(new Person("John", "Smith", 15));
People.Add(new Person("Caity", "Johnson", 13));
People.Add(new Person("Jody", "Patterson", 39));
People.Add(new Person("Chloe", "Dog", 30));
People.Add(new Person("Corey", "Patterson", 18));
For Example, to filter the collection and display only my family members requires a single line of code
delegate(Person person)
{
return person.LastName == “Patterson”;
});
And produces the following output
To remove the teenagers (not a bad proposition) also requires a single line
delegate(Person person)
{
return person.Age > 12 && person.Age < 20; });
}
Sorting
Sorting requires a Comparison<T> delegate instead of Predicate<T> but operates in much the same fashion.
For example, sorting by last name requires this single line of code:
delegate(Person x, Person y)
{
return x.LastName.CompareTo(y.LastName);
});
a slight modification will sort the list in descending order:
delegate(Person x, Person y)
{
return y.LastName.CompareTo(x.LastName);
});
To sort by Last Name then First Name would look something like this:
delegate(Person x, Person y)
{
if (x.LastName == y.LastName)
return x.FirstName.CompareTo(y.FirstName);
else
return x.LastName.CompareTo(y.LastName);
});
Conclusion
I’ve just scratched the surface on what the generic List collection is capable of providing. There are several other generic methods that take delegates as parameters, such as ForEach and ConvertAll, that open up interesting possibilities in your code. Overall I find the Anonymous Method syntax a bit kludgy but C# 3.0 promises to reduce the syntactical noise and give us true (almost) Lambda Expressions.











Related Articles
9 users responded in this post
that’s brillant
This is all wrong man.
This is all wrong man. Unreal dude.
Can you show me how to do it in Visual Basic?
How would you go about unit testing delegate methods?
"People.RemoveAll(delegate(Person person) { return person.Age > 12 && person.Age < 20; });"
Using your example, what we have is a simple way to prevent creating a new method (ie the IsTeen(person) method). But, as the delegate methods get complex it becomes difficult to read, debug, or test. At what point does it become too complex to use this approach? Is there a rule of thumb?
Another thing to remember when using anonymous methods, is that Debug-Continue will not work in Visual Studio. Just something to keep in mind
I agree that this is a most compelling feature and will reduce code amount and complexity. However I wonder whether the code becomes too simple. E.g. what about null checks (will NullReferenceException replace ArgumentNullException?). What about consistency (e.g. in one place using case sensitive comparison, case insensitive in the other. Identifying a person by name in onle place. Also checking the birth date in the other.)
Due to lambdas we _will_ see this code more often. To the good or the bad? I don’t know.
It is not a requirement to use anonymous methods for Predicate<T> or Comparison<T> parameters but, when it makes sense, it produces concise and easily readable code. However, when anonymous methods are misused it can produce code that is difficult to maintain. Like most features, the “trick” is determining when it becomes a benefit and when it becomes the bane of your existence.
When to use Anonymous methods:
1. Single use code snippet
2. Logic is less than 5 lines of code.
3. The intent can clearly be seen in the code, otherwise a method name would better express intent (i.e method name of SortByLastNameThenFirstName)
When <B>not</B> to use Anonymous methods:
1. Code is used multiple times.
2. Requires a Unit Test
3. Code is complex enough to obfuscate intent with a quick review.
These are just guidelines and you will, no doubt, amend them as you become more comfortable using anonymous methods
Exactly what I was looking for. Thanks for posting this, Randy!
I couldn’t figure out the "Predicate" idea…was sorting through my List Collection from top to bottom.
This idea dramatically sped things up.
I did get a little snagged because I wanted to use a FIND.
Kept trying to put the findings to a LIST, but it needed to return one of the List’s objects.
I had already figured out the use of Sorting delegates a couple days ago.
I have a delegate with 6 levels of sorting and it is very fast.
I’m still a little baffled on how the Sort "works"…I’m just glad I don’t have to write it.
I remember writing those routines in CompSci. Ugh!
Jeff P.
Leave A Reply