Why limit it to
IList
, instead of
IEnumerable
?
public static class ListExtension
{
public static void ForEach<t>(this IEnumerable<t> list, Action<t> function)
{
foreach (T item in list)
{
function(item);
}
}
}
---
I believe that the reason it is not an
IEnumerable
extension is they did not want operations that could mutate data as part of the library, and there was not enough benefit to justify, it is simple enough to write your own.
---
Also (in response to #2) using the extensions, makes it semantically clearer.
var studentList = new List<student>();
foreach (var st in studentList.Where(st => st.Age != 0))
{
st.AgeInMonths = DateTime.Now.Subtract(st.DOB).TotalDays / 30)
}
or
studentList.Where(st => st.Age != 0)
.ForEach(st =>{ st.AgeInMonths = DateTime.Now.Subtract(st.DOB).TotalDays / 30); });
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.