We use for or foreach to loop through collections and process each entries.
Is there any alternative in all those new lambda functions for collections in C#?
Traditional way of doing
foreach(var v in vs)
{
Console.write(v);
}
Is there anything like?
vs.foreach(v => console.write(v))
List has the ForEach method, however, IEnumerable does not.
There are a number of questions / answers regarding this. I think the main reason it was not implemented in IEnumerable though is that Linq on Enumerables is “meant” to be side effect free as it’s a querying language.
Eric Lippert explains his thoughts on his blog.
http://blogs.msdn.com/ericlippert/archive/2009/05/18/foreach-vs-foreach.aspx