Click here to Skip to main content
15,892,298 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

ForEach extension on IList

Rate me:
Please Sign up or sign in to vote.
5.00/5 (2 votes)
2 Oct 2011CPOL 24.3K   1   10
The List class already has a method doing the same job as:public static void ForEach(this IList list, Action function)More about the ForEach method is available here. Here is the reflected ForEach method:public void ForEach(Action action){ if (action == null) { ...

The List<t> class already has a method doing the same job as:


C#
public static void ForEach<t>(this IList<t> list, Action<t> function)

More about the ForEach method is available here. Here is the reflected ForEach method:


C#
public void ForEach(Action<T> action)
{
  if (action == null)
  {
    ThrowHelper.ThrowArgumentNullException(ExceptionArgument.match);
  }
  for (int i = 0; i < this._size; i++)
  {
    action(this._items[i]);
  }
}

to show the inner implementation of .NET ForEach() of List<t>. So we can use this .NET built in ForEach method to do the job, for example, if we want to process List<t> elements, we can try:


C#
List<string> myList = new List<string>() { "One", "Two" };
myList.ForEach(item => Console.WriteLine(item));

and with the Where clause:


C#
myList.Where(item => item == "One").ToList().ForEach(item => Console.WriteLine(item));

From the above code, we can see the Where clause will return an IEnumerable<t> object, so we cast it to List<t> using ToList() and then apply the ForEach method over it.


And also over IList<t> elements:


C#
IList<string> myList = new List<string>() { "One", "Two" };
myList.ToList().ForEach(item => Console.WriteLine(item));
myList.Where(item => item == "One").ToList().ForEach(item => Console.WriteLine(item));

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
Australia Australia

Comments and Discussions

 
GeneralRe: You're right that it compiles but what would you do with the... Pin
Reto Ravasio11-Oct-11 9:27
Reto Ravasio11-Oct-11 9:27 
You're right that it compiles but what would you do with the old method? If you leave it, intelisense will show you two methods and when you remove it it could be considered a breaking change because without the new namespace old code will not compile. I'm sure they had some discussions about this at Microsoft before they released .NET3.5. Whichever way they decide, there are always people not happy with the outcome. Overall I think they did an excellent job with the BCL.
GeneralRe: They could add it as an extension to IEnumerable<T> without ... Pin
Henry.Ayoola11-Oct-11 0:05
Henry.Ayoola11-Oct-11 0:05 
GeneralRe: Thanks mate, good stuff :) Pin
Mohammad A Rahman23-Sep-11 2:13
Mohammad A Rahman23-Sep-11 2:13 
GeneralRe: Neither i disagree on your alternative...really appreciate f... Pin
kris44422-Sep-11 18:20
kris44422-Sep-11 18:20 
GeneralRe: Yes, that's the same point i mentioned...when you call .ToLi... Pin
kris44422-Sep-11 18:19
kris44422-Sep-11 18:19 
GeneralThis really should have been an extension method for IEnumer... Pin
Henry.Ayoola23-Sep-11 3:28
Henry.Ayoola23-Sep-11 3:28 
GeneralRe: I agree but how could they have prevented that situation? Li... Pin
Reto Ravasio7-Oct-11 22:45
Reto Ravasio7-Oct-11 22:45 
GeneralHi But, when you perform ToList() it actually copies to new ... Pin
kris44421-Sep-11 17:42
kris44421-Sep-11 17:42 
GeneralRe: Thanks, in here I found something about ToList() http://stac... Pin
Mohammad A Rahman21-Sep-11 23:09
Mohammad A Rahman21-Sep-11 23:09 
GeneralRe: And also I just suggested an alternative, thats all :) Pin
Mohammad A Rahman21-Sep-11 23:09
Mohammad A Rahman21-Sep-11 23:09 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.