in

ASP.NET Weblogs

This Blog

Syndication

Christian Nagel's OneNotes

.NET Training, Consulting, Coaching - C#, Web Services, Enterprise Services, ASP.NET, Whidbey, Longhorn and More!

Generic Delegates and Anonymous Methods

Generics and anonymous methods are great improvements of C#.

Having a generic collection persons of type List<Person>, the objects of the collection can be accessed with a simple foreach statement:

foreach (Person p in persons)
{
   Console.WriteLine(p);
}

The collection class also has a ForEach method that can be used instead of a foreach statement. The ForEach method has the parameter Action<T> action. Action is a generic delegate:
public delegate void Action<T>(T obj);

Using Person objects in the collection, the method that can be invoked by the delegate looks like this:

void DisplayPerson(Person p)
{
   Console.WriteLine(p);
}

Now this method can be passed to the ForEach method:

persons.ForEach(new Action<Person>(DisplayPerson));

The same can be done with fewer characters as the delegate type is inferred if possible:

persons.ForEach(DisplayPerson);

If the method that is invoked by ForEach is not used on other places, it can be implemented as an anonymous method:

persons.ForEach(delegate(Person p)
{
   Console.WriteLine(p);
});

Christian

Published Mar 04 2005, 03:16 PM by CNagel
Filed under: ,

Comments

 

TrackBack said:

March 6, 2005 4:52 AM
 

TrackBack said:

March 11, 2005 6:34 PM
 

TrackBack said:

April 19, 2005 9:58 PM
 

TrackBack said:

April 27, 2005 10:35 AM
 

TrackBack said:

I am currently preparing my developer track talks for the local Big&gt;Days 2005 road show and - oh what...
May 9, 2005 4:24 AM
 

A Skeptic said:

Combining generics with delegates and then adding anonymous methods on top of it does a real number on the maintainability of the code. What do you think? I think this code is very hard to understand.

December 26, 2007 12:00 PM
 

Tomas Scheel said:

@ A Skeptic

This is a fairly simple example, but with .NET 3.5 it gets simpler... The last bit:

persons.ForEach(delegate(Person p)

{

  Console.WriteLine(p);

});

Can be rewritten as

persons.ForEach(p => Console.WriteLine(p));

I think that is far easier on code maintainability. In addition, it looks a lot cleaner. I like small bits like what is above, but with everything else, only in moderation. Something like:

persons.ForEach(p =>

{

// 30 lines later....

});

Is generally a bad idea.

I especially like this for Find and Exists, for example:

persons.Exists(p => p.FirstName == "Bob");

December 26, 2007 7:21 PM
 

Shahid Riaz Bhatti said:

@ Tomas Scheel

your solution is good, but the example which u gave later i.e. like this one "persons.Exists(p => p.FirstName == "Bob");" can not be implemented in .Net 2.0.

The original post is excellent post if you are working in .Net 2.0 environment, the reason is that I have seen many developers using loops instead of using delegates..

Tomas your example is good but only if some one is working in 3.5 environment, because lambdas expression are not supported in the earlier version..

but at the end I must say that I like this post, it was really helpfull.

Thanx for sharing

October 29, 2008 12:06 AM
 

Work from home moms. said:

Site build it work at home moms wahm. Moms work at home. Work at home moms resource wahmpros.

November 7, 2008 5:58 AM

Leave a Comment

(required)  
(optional)
(required)  
Add