Click here to Skip to main content
Click here to Skip to main content
Alternative Tip

Tagged as

ForEach extension on IList

, 2 Oct 2011
Rate this:
Please Sign up or sign in to vote.
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.

License

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

Share

About the Author

Doc Lobster
Software Developer
Germany Germany
No Biography provided

Comments and Discussions

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

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web04 | 2.8.140827.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid