Call List<T>.ForEach()
. Pass it a lambda.
Enumerable.Range(0,9).ToList().ForEach( n => Console.WriteLine(n) );
UPDATE
Wait a minute, are you trying to iterate not over your list of objects, but over a list of PROPERTIES in each object in the list (DayOne, DayTwo, etc.), and possibly also automagically link them up with variables with similar names? No, there's nothing like that in C#. You'd have to roll your own using reflection, and that would be a lot of work for very little payoff.
You could consider storing those values not as properties, but in a Dictionary<String,String>
, or perhaps an array String DayValues[]
; either of those would support iteration, but you'd still have the problem of linking up the items programmatically with the label controls. You might be able to look up the controls by name, though, depending how you're doing your UI -- ASP.NET has FindControl(string id)
, for example.
But what you have above is a perfectly clear, readable way of doing it, just the way it is. That's how I'd do it in production code, except that I'd assign list[x]
to a local variable at the top of the loop block to clear up the visual noise a bit. An iterating lambda solution would sacrifice clarity for cleverness, not a good tradeoff in code you'll have to maintain. If it's just fun code, on the other hand, by all means stretch out and learn something new. Lambdas are a fantastic toy.
If that's really the question you're asking, you should edit your question for clarity.