Mam problem z anonimową metodą w pętli.Problem z innym "kontekstem wykonania" anonimowej metody w pętli
Poniższy kod jest tylko do zilustrowania mój 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);
}
A kiedy kliknij w przycisk, wyjście jest:
Victor Wooten
Victor Wooten
Victor Wooten
Victor Wooten
zamiast:
Jaco Pastorius
Marcus Miller
Flea
Vicor Wooten
Głównym punktem mojego problemu jest wykonanie zróżnicowanych kontekst. Wiem, że mój przykład jest głupi.
Jaco Pastorius, Marcus Miller, Flea and Victor Wooten. Jeden z nich nie jest taki jak inne ... – jason
4 różne style ... ale lubię je wszystkie! – Florian