Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#

ForEach extension on IList

4.67/5 (5 votes)
2 Oct 2011CPOL 10.8K  
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:

C#
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.

License

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