Click here to Skip to main content
16,021,041 members
Articles / Programming Languages / C#
Alternative
Tip/Trick

ForEach extension on IList

Rate me:
Please Sign up or sign in to vote.
4.67/5 (5 votes)
2 Oct 2011CPOL 10.8K   5
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)


Written By
Software Developer
Germany Germany
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralReason for my vote of 5 You had me at "fancy lambda stuff" Pin
dmjm-h25-Oct-11 13:44
dmjm-h25-Oct-11 13:44 
GeneralActually the original version should be around ~156 characte... Pin
Reto Ravasio7-Oct-11 22:22
Reto Ravasio7-Oct-11 22:22 
GeneralRe: Thats right. Pin
Doc Lobster7-Oct-11 22:38
Doc Lobster7-Oct-11 22:38 
GeneralReason for my vote of 4 Nice Pin
kiran dangar4-Oct-11 19:13
kiran dangar4-Oct-11 19:13 
GeneralReason for my vote of 5 When as in this case foreach and lam... Pin
Member 818188226-Sep-11 21:49
Member 818188226-Sep-11 21:49 

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.