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

ForEach extension on IList

By , 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<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.


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

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.140415.2 | Last Updated 2 Oct 2011
Article Copyright 2011 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid