Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

ForEach extension on IList

By , 2 Oct 2011
Rate this:
Please Sign up or sign in to vote.

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

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:

public void ForEach(Action<T> action)
  if (action == null)
  for (int i = 0; i < this._size; 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:

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

and with the Where clause:

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:

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));


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

About the Author

Mohammad A Rahman
Software Developer
Australia Australia
Designer and Architect.
Author of the Expert C# 5.0: with the .NET 4.5 Framework book

Comments and Discussions

GeneralRe: You're right that it compiles but what would you do with the... PinmemberReto Ravasio11-Oct-11 9:27 
GeneralRe: They could add it as an extension to IEnumerable<T> without ... PinmemberHenry.Ayoola11-Oct-11 0:05 
GeneralRe: Thanks mate, good stuff :) PinmemberMohammad A Rahman23-Sep-11 2:13 
GeneralRe: Neither i disagree on your alternative...really appreciate f... Pinmemberkris44422-Sep-11 18:20 
GeneralRe: Yes, that's the same point i mentioned...when you call .ToLi... Pinmemberkris44422-Sep-11 18:19 
GeneralThis really should have been an extension method for IEnumer... PinmemberHenry.Ayoola23-Sep-11 3:28 
GeneralRe: I agree but how could they have prevented that situation? Li... PinmemberReto Ravasio7-Oct-11 22:45 
GeneralHi But, when you perform ToList() it actually copies to new ... Pinmemberkris44421-Sep-11 17:42 
GeneralRe: Thanks, in here I found something about ToList() http://stac... PinmemberMohammad A Rahman21-Sep-11 23:09 
GeneralRe: And also I just suggested an alternative, thats all :) PinmemberMohammad A Rahman21-Sep-11 23:09 

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

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

| Advertise | Privacy | Mobile
Web03 | 2.8.140415.2 | Last Updated 2 Oct 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid