C# iterate list for objects without some fields -
how can iterate list<> of objects having 2 of 4 fields? below code works far there easier way in c#?
using system; using system.collections.generic; namespace consoleapplication1 { class program { private static void main() { var list = new list<employee> { new employee("a", 32, 5235.32, 2004, 3, 2), new employee("b", 28, 1435.43, 2011, 11, 23), new employee("c", 47, 3416.49, 1997, 5, 17), new employee("d", 22), new employee("e", 57) }; list.foreach(l => { if (l.salary == 0) console.writeline(" {0} {1}", l.name, l.age); }); } } }
it looks you're trying filter items out of collection - you're doing works fine, it's (arguably) more idiomatically written as:
foreach(var l in list.where(x => x.salary == 0)) { console.writeline(" {0} {1}", l.name, l.age); }
Comments
Post a Comment