Reputation: 554
I'm trying to address all items of a list without a forEach-loop.
For example:
ColorTextBlocks = new List<TextBlock>()
{
tb1,
tb2,
tb3,
tb4,
tb5,
tb6
}
Is it possible to change the foreground of all items in one line?
Upvotes: 0
Views: 74
Reputation: 273524
Sure, you have the List<T>.ForEach()
method.
ColorTextBlocks.ForEach(tb => tb.Color = Color.Green);
But it is debatable whether this is any 'better' than a plain foreach() loop. It certainly won't be faster.
Upvotes: 3
Reputation: 8452
Sure:
ColorTextBlocks.All(c => { c.ForeGround = Color.Blue; return true; });
You can also do:
ColorTextBlocks.ForEach(c => c.ForeGround = Color.Blue);
But why "in one line"? A foreach loop is traditionally more readable that Linq queries...
Upvotes: 2