Click here to Skip to main content
11,709,120 members (65,756 online)
Click here to Skip to main content

Tagged as

ForEach extension on IList

, 2 Oct 2011 CPOL 5.9K 1
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


You may also be interested in...

Comments and Discussions

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