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

Tagged as

ForEach extension on IList

, 2 Oct 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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)
  {
    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:

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

License

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

Share

About the Author


Comments and Discussions

 
GeneralRe: You're right that it compiles but what would you do with the... PinmemberReto Ravasio11-Oct-11 10:27 
GeneralRe: They could add it as an extension to IEnumerable<T> without ... PinmemberHenry.Ayoola11-Oct-11 1:05 
GeneralRe: Thanks mate, good stuff :) PinmemberMohammad A Rahman23-Sep-11 3:13 
GeneralRe: Neither i disagree on your alternative...really appreciate f... Pinmemberkris44422-Sep-11 19:20 
GeneralRe: Yes, that's the same point i mentioned...when you call .ToLi... Pinmemberkris44422-Sep-11 19:19 
GeneralThis really should have been an extension method for IEnumer... PinmemberHenry.Ayoola23-Sep-11 4:28 
GeneralRe: I agree but how could they have prevented that situation? Li... PinmemberReto Ravasio7-Oct-11 23:45 
GeneralHi But, when you perform ToList() it actually copies to new ... Pinmemberkris44421-Sep-11 18:42 
GeneralRe: Thanks, in here I found something about ToList() http://stac... PinmemberMohammad A Rahman22-Sep-11 0:09 
GeneralRe: And also I just suggested an alternative, thats all :) PinmemberMohammad A Rahman22-Sep-11 0: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 | Terms of Use | Mobile
Web04 | 2.8.141223.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Mohammad A Rahman
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid