I have a situation where I have someFunction(int), and I need to generate programmatically n buttons that will call it. What this means is that I want to create buttons B1, B2, ... Bn that call someFunction(1), someFunction(2), ... someFunction(n) when clicked.
This is how I attempted to do this (semi-pseudocode):
for (int i = 1; i <= n; i++) {
  Button b = new Button();
  b.Caption = "Value " + n; // non-WPF: b.Text = "Value " + n;
  b.Click += (sender, event) => {
    someFunction(i);
  }
}
What bugs me about this is that when I click on the first button (B1), with a debugger over someFunction(i), it tells me that it's calling someFunction(n + 1).
I'm not sure why this is, or how to fix it. The work-around I use is to use, instead of someFunction(i), someFunction(int.Parse(i.ToString()) (to create a copy of i). But this seems shady to me, because integers should be value types.
 
     
     
    