I have a problem with an anonymous method in a loop.
The following code is just to illustrate my problem:
private void Form1_Load(object sender, EventArgs e) { List<string> bassists = new List<string>(){ "Jaco Pastorius", "Marcus Miller", "Flea", "Vicor Wooten" }; foreach (string item in bassists) { this.button1.Click += (s, ea) => Output(s, ea, item); } } private void Output(object s, EventArgs e, string item) { this.listBox1.Items.Add(item); }
And when I click the button, the output is:
Victor Wooten
Victor Wooten
Victor Wooten
Victor Wooten
instead:
Jaco Pastorius
Marcus Miller
Flea
Vicor wooten
The highlight of my problem is the context of making the differences. I know my example is stupid.
Florian
source share