advanced web statistics

List.ForEach Method & Delegate Trick

4/26/2007 11:56:18 PM

Andrew Robinson sent out an e-mail to our local .NET group about anonymous delegates.  The e-mail got me interested and I started reading up on delegates in the 2.0 framework. I am currently working on a class for a project that uses a lot of Generic lists.  While enumerating these lists I need to make changes to the collection and of course I am unable to make direct changes to the collection while I am enumerating (rightfully so!).  The next logical thing to do is create a copy and make changes and after enumerating commit those changes to the collection.  This entailed creating a method such as CopyList that would take a parameter of List<T> or simply an Object.

Two weeks ago I had a method that looked like the following.

static List<Bar> CopyFoo(Foo item)
{
   List<Bar> foo = new List<Bar>();

   foreach (Bar bar in item.Bar)
      foo.Add(bar);

   return foo;
}

Pretty basic stuff. The method takes the Foo object and enumerates each Bar in item. Each Bar is added to another collection of Bar. Pretty simple and straight-forward.  As simplistic as that looks, there is actually an easier, and in my opinion; more elegant way.

With the Generic List ForEach method you can dump the entire contents of a collection in one line!

static List<Bar> CopyFoo(Foo item)
{
   List<Bar> foo = new List<Bar>();

   item.Bar.ForEach(delegate (Bar bar) {foo.Add(bar);});

   return foo;
}

Very simple but a cool trick nonetheless.  The action of the ForEach method is a delegate to a method that performs an action (List.Add) on the object (Bar) passed to it.

.NET, C#, Interesting, Programming

kick it on DotNetKicks.com

Comments


Of course nowadays you can do:

item.Bar.ForEach(bar => foo.Add(bar));

Posted by: Maurits | 11/21/2007 6:43:01 AM

Very true Maurits. This is specific to 2.0.

Posted by: Will Asrari | 11/21/2007 1:09:15 PM

works very well !!!

Posted by: Prat | 5/14/2008 2:21:46 AM

cara, gostei muito do seu blog....

Posted by: Sites | 10/10/2008 9:01:04 PM

<quote>
item.Bar.ForEach(bar => foo.Add(bar));
</quote>

or even cleaner:

item.Bar.ForEach(foo.Add);

Posted by: jim | 12/3/2008 4:10:18 PM

Great Post! Really very useful information is given.

Posted by: .net development | 7/22/2009 2:24:08 AM

http://diditwith.net/2006/10/05/PerformanceOfForeachVsListForEach.aspx

Posted by: ManuelC | 8/28/2009 5:38:14 AM

interesting post thanks
i really like it

Posted by: Panic Attacks | 9/11/2009 5:17:44 AM

keep working like this thanks!!

Posted by: Blue Eyes | 9/18/2009 4:51:03 AM

intresting blog fellas keep it on

2

Posted by: Buy Viagra | 9/18/2009 10:13:12 AM

Thanks for this helpful information.

Posted by: Generic Cialis Order | 10/5/2009 4:54:05 PM

hey i think that is a good idea keep up the good work

Posted by: Discount Cialis | 10/5/2009 4:55:17 PM

I really like your blog has great variety of points of view and I like the theme and congratulations

Posted by: Buy Viagra | 11/5/2009 12:43:57 PM

I liked this blog very creative and I really like the views of the focus very good indeed

Posted by: Generic Medications | 11/6/2009 3:37:18 PM

With .NET 3.5 you could just use the System.Linq.Enumerable.ToList<TSource> method

Posted by: Meo | 11/26/2009 6:18:36 AM

I like Maurits coding better item.Bar.ForEach(bar => foo.Add(bar));

...kind of closer to me since I am more use to newer studio solutions (`cause I finished college just a year ago)....

Posted by: Paul | 12/16/2009 10:42:37 AM

Leave a Comment

   

  Enter the text to proceed!