by Jesper Persson
20. August 2009 10:31
I got an interesting question today. "Is it possible to assign values to more then one property in a collection. Using List.ForEach(Action<T>) and lambda expressions?"
Well how hard can that be so I naturally googled it, and came up with no usable results what so ever, I dont blame Google here might be me thats using the wrong search expression. The closest I could get was this article on MSDN: http://msdn.microsoft.com/en-us/library/bb397687.aspx
I dont think this is something you do every day, and I have a hard time figuring out why this was desireable but then again i did'nt ask.
First we need a class:
class test
{
public int prop1 { get; set; }
public int prop2 { get; set; }
}
Next a small console application:
class Program
{
static void Main(string[] args)
{
List<test> l = new List<test>();
l.Add(new test());
l.Add(new test());
l.ForEach(p => { p.prop1 = 2; p.prop2 = 4; });
foreach (test item in l)
{
Console.WriteLine(item.prop1 + item.prop2);
}
Console.ReadLine();
}
}