I just stumbled across the new .ForEach methods in .NET 2.0 for Array and the new generic collection objects. Instead of:
GeekWithBlog[] geeks = GetGeekArray();
foreach (GeekWithBlog geek in geeks)
{
RaiseSalary(geek);
}
}
private void RaiseSalary(GeekWithBlog geek)
{
geek.Salary *= 2;
}
... you can now code:
GeekWithBlog[] geeks = GetGeekArray();
Array.ForEach(geeks, RaiseSalary);
}
private void RaiseSalary(GeekWithBlog geek)
{
geek.Salary *= 2;
}
Combined with anonymous methods, this could either make for sleeker code. (Or, it could make for Perl-style "write-only" code if overdone.)
Update: D'oh! My first post and I was so busy trying to make it look pretty that both code examples were exactly the same. Thanks to those who pointed it out nicely (OK, starting NOW, don't call me Mort). The second example now shows the Array.ForEach method, as I intended.