Lately I've read C# in Depth and it taught me about lambda expression, I've been using them to pas data to click events and such like:
image.MouseDown+=(o ,e)=>MethodToDoSomething(DataNeededForAction);
now the problem with those is variable capturing when used in a foreach loop (thank you Jon Skeet for making that part really clear :), when initializing several objects that have events that I subscribe to I usually bump into an issue with variable capturing. consider the following example:
foreach (var game in GamesCollection)
{
Image img = new Image();
img.Width = 100;
img.MouseDown+=(o,e) => MyMethod(game.id);
}
To avoid capturing in this situation I have to add some variable to assign game to and then pass that variable to the method, this creates extra unclear code and mostly, additional clutter. Is there a way to bypass this? Something that'll at least look cleaner?
Thx, Ziv