Advertisement
pszczyg

Untitled

Apr 29th, 2016
888
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.81 KB | None | 0 0
  1.         public void InternalsTest_VariableOutsideLoop()
  2.         {
  3.             var strings = new List<string> {"one", "two", "three"};
  4.             var buttons = new List<Button>();
  5.  
  6.             var displayClass = new DisplayClass();
  7.            
  8.             for (int index = 0; index < strings.Count; index++)
  9.             {
  10.                 displayClass.str = strings[index];
  11.                 var btn = new Button();
  12.                 btn.Click += displayClass.action;
  13.                 buttons.Add(btn);
  14.             }
  15.  
  16.             buttons.ForEach(x => x.PerformClick());
  17.         }
  18.  
  19.         private sealed class DisplayClass
  20.         {
  21.             public string str { get; set; }
  22.  
  23.             public void action(object sender, EventArgs e)
  24.             {
  25.                 MessageBox.Show(str);
  26.             }
  27.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement