65.9K
CodeProject is changing. Read more.
Home

ForEach extension on IList

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.67/5 (5 votes)

Sep 22, 2011

CPOL
viewsIcon

10937

Despite the existance of all that fancy lambda stuff, I would still write this the old-school way:var studentList = new List();foreach (var st in studentList) { if (st.Age == 0) st.AgeInMonths = DateTime.Now.Subtract(st.DOB).TotalDays / 30);} My "solution" needs ~160...

Despite the existance of all that fancy lambda stuff, I would still write this the old-school way:
var studentList = new List<student>();

foreach (var st in studentList) {
   if (st.Age == 0) st.AgeInMonths = DateTime.Now.Subtract(st.DOB).TotalDays / 30);
} 
My "solution" needs ~160 characters versus ~174 characters in the op version (without the generic method). I also think it's easier to read for the majority of people - and that's the important point.