title: Problem with loop optimization or closure of lambda?
body: <p>In the following method I'm sending an enumeration of actions and want an array of ICommands back that call <code>Action&lt;object&gt;</code> that wrap those actions (needed for the relayCommand).</p>
  
  <p>The problem is that if I do this inside the for each (or even a for loop) I get commands that always execute the first action passed in the parameters.</p>
  
  <pre><code>    public static ICommand[] CreateCommands(IEnumerable&lt;Action&gt; actions)
      {
          List&lt;ICommand&gt; commands = new List&lt;ICommand&gt;();
  
          Action[] actionArray = actions.ToArray();
  
          // works
          //commands.Add(new RelayCommand(o =&gt; { actionArray[0](); }));  // (_execute = {Method = {Void &lt;CreateCommands&gt;b__0(System.Object)}})
          //commands.Add(new RelayCommand(o =&gt; { actionArray[1](); }));  // (_execute = {Method = {Void &lt;CreateCommands&gt;b__1(System.Object)}})
  
          foreach (var action in actionArray)
          {
              // always add the same _execute member for each RelayCommand (_execute = {Method = {Void &lt;CreateCommands&gt;b__0(System.Object)}})
              commands.Add(new RelayCommand(o =&gt; { action(); }));
          }
  
          return commands.ToArray();
      }
  </code></pre>
  
  <p>It seems that the lambda is always reused inside the loop thinking it does the same, but it does not.</p>
  
  <p>How do I overcome this situation?
  How can i force the loop to threat <code>o =&gt; { action(); }</code> always as a new one?</p>
  
  <p>Thanks!</p>
  
  <p>What I tried as per suggestions, but did not help:</p>
  
  <pre><code>        foreach (var action in actionArray)
          {
              Action&lt;object&gt; executeHandler = o =&gt; { action(); };
              commands.Add(new RelayCommand(executeHandler));
          }
  </code></pre>
  
  <p>What seems to work for me is:</p>
  
  <pre><code>    class RelayExecuteWrapper
      {
          Action _action;
  
          public RelayExecuteWrapper(Action action)
          {
              _action = action;
          }
  
          public void Execute(object o) 
          {
              _action();
          }
      }
  
  /// ...
      foreach (var action in actionArray)
      {
          RelayExecuteWrapper rxw = new RelayExecuteWrapper(action);
          commands.Add(new RelayCommand(rxw.Execute));
      }
  </code></pre>
  
  <p>Code of RelayCommand:</p>
  
  <pre><code>/// &lt;summary&gt;
  /// A command whose sole purpose is to 
  /// relay its functionality to other
  /// objects by invoking delegates. The
  /// default return value for the CanExecute
  /// method is 'true'.
  /// &lt;/summary&gt;
  public class RelayCommand : ICommand
  {
      #region Fields
  
      readonly Action&lt;object&gt; _execute;
      readonly Predicate&lt;object&gt; _canExecute;        
  
      #endregion // Fields
  
      #region Constructors
  
      /// &lt;summary&gt;
      /// Creates a new command that can always execute.
      /// &lt;/summary&gt;
      /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt;
      public RelayCommand(Action&lt;object&gt; execute)
          : this(execute, null)
      {
      }
  
      /// &lt;summary&gt;
      /// Creates a new command.
      /// &lt;/summary&gt;
      /// &lt;param name="execute"&gt;The execution logic.&lt;/param&gt;
      /// &lt;param name="canExecute"&gt;The execution status logic.&lt;/param&gt;
      public RelayCommand(Action&lt;object&gt; execute, Predicate&lt;object&gt; canExecute)
      {
          if (execute == null)
              throw new ArgumentNullException("execute");
  
          _execute = execute;
          _canExecute = canExecute;           
      }
  
      #endregion // Constructors
  
      #region ICommand Members
  
      [DebuggerStepThrough]
      public bool CanExecute(object parameter)
      {
          return _canExecute == null ? true : _canExecute(parameter);
      }
  
      public event EventHandler CanExecuteChanged
      {
          add { CommandManager.RequerySuggested += value; }
          remove { CommandManager.RequerySuggested -= value; }
      }
  
      public void Execute(object parameter)
      {
          _execute(parameter);
      }
  
      #endregion // ICommand Members
  }
  </code></pre>