Click here to Skip to main content
12,453,183 members (62,468 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

4.7K views
Posted

ForEach extension on IList

, 2 Oct 2011 CPOL
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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralReason for my vote of 5 You had me at "fancy lambda stuff" Pin
dmjm-h25-Oct-11 13:44
memberdmjm-h25-Oct-11 13:44 
GeneralActually the original version should be around ~156 characte... Pin
Reto Ravasio7-Oct-11 22:22
memberReto Ravasio7-Oct-11 22:22 
GeneralRe: Thats right. Pin
Doc Lobster7-Oct-11 22:38
memberDoc Lobster7-Oct-11 22:38 
GeneralReason for my vote of 4 Nice Pin
kiran dangar4-Oct-11 19:13
memberkiran 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
memberMember 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.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160826.1 | Last Updated 2 Oct 2011
Article Copyright 2011 by Doc Lobster
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid