Click here to Skip to main content
13,248,766 members (55,301 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


Posted 21 Sep 2011

ForEach extension on IList

, 2 Oct 2011
Rate this:
Please Sign up or sign in to vote.
ForEach extension for the IList

Very often, we have the requirement to perform some sort of action on each entity in a List. For example, from a student list, I need to update the Age of the student whose Age is 0.

I can do it like this:

var studentList = new List<student>();
studentList.Where<student>(s => s.Age == 0).ForEach<student>(
  st => st.AgeInMonths = DateTime.Now.Subtract(st.DOB).TotalDays / 30);

For this to work, these are the extension methods needed:

public static class ListExtension
    public static void ForEach<t>(this IList<t> list, Action<t> function)
        foreach (T item in list)
    public static void ForEach<t>(this IEnumerable<t> list, Action<t> function)
        foreach (T item in list)


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


About the Author

India India
No Biography provided

You may also be interested in...

Comments and Discussions

GeneralIList<T> implements IEnumerable<T>, so you don't need the IL... Pin
Richard Deeming3-Oct-11 10:33
memberRichard Deeming3-Oct-11 10:33 

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.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.171114.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by kris444
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid