title: Relaycommand and parameterless execute method
body: <p>I'm currently learning WPF and MVVM, I think I get most of it and how it works but I've come across something on using the RelayCommand (or DelegateCommand) that I don't understand.  I think it's to do with the way delegates work.</p>
  
  <p>Please note that the code below is all just in test solutions at the moment, so no live code.  Also I am considering this for commands that do not require a parameter such as close and to understand why it works.</p>
  
  <p>So if I take the RelayCommand that Josh Smith created( <a href="http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030" rel="nofollow noreferrer">http://msdn.microsoft.com/en-us/magazine/dd419663.aspx#id0090030</a>) I can setup a command like this:</p>
  
  <pre><code>RelayCommand updateTextContentCommand;
  
  public ICommand UpdateTextContentCommand
  {
      get
      {
          if (updateTextContentCommand == null)
          {
              updateTextContentCommand = new RelayCommand(
                  param =&gt; this.UpdateTextContentCommand_Execute());
          }
          return updateTextContentCommand;
      }
  }
  </code></pre>
  
  <p>with this execute method:</p>
  
  <pre><code>public void UpdateTextContentCommand_Execute()
  {
      this.TextContent = DateTime.Now.ToString();
  }
  </code></pre>
  
  <p>I used a simple binding to a TextBlock to see the result and the command is bound to a button.  This works fine.  What I don't get is the use of the lambda expression to create the command.  The <code>Action&lt;object&gt;</code> expects a parameter doesn't it? so why does this code work?</p>
  
  <p>If I change the code above to </p>
  
  <pre><code>if (updateTextContentCommand == null)
  {
      updateTextContentCommand = new RelayCommand(
          this.UpdateTextContentCommand_Execute());
  }
  </code></pre>
  
  <p>I get these error:</p>
  
  <p>*The best overloaded method match for 'MVVM.RelayCommandTesting.Framework.RelayCommand.RelayCommand(System.Action)' has some invalid arguments</p>
  
  <p>Argument 1: cannot convert from 'void' to 'System.Action'*</p>
  
  <p>and removing the <code>()</code> after Execute gives this error:</p>
  
  <p><em>Argument 1: cannot convert from 'method group' to 'System.Action'</em></p>
  
  <p>But if I change the code like this:</p>
  
  <pre><code>if (updateTextContentCommand == null)
  {
      updateTextContentCommand = new RelayCommand(
          this.UpdateTextContentCommand_Execute);
  }
  
  public void UpdateTextContentCommand_Execute(object param)
  {
      this.TextContent = DateTime.Now.ToString();
  }
  </code></pre>
  
  <p>it complies and runs fine.  If I change the view to use CommandParameter I can then use param to set the text content using this method but if I use the lambda style I have to pass a parameter to the line so its like this param => <code>this.UpdateTextContentCommand_Execute(param)</code>.  </p>
  
  <p>In my test I'm hard coding the CommandParameter value but I guess it would most likely be data bound to a property of the ViewModel in a real system so you would be able to pass the parameter in the lambda style.</p>
  
  <p>Can anyone explain why the parameterless version works with the lambda style please?</p>
  
  <p>Thanks for taking the time to read this.</p>
  
  <p>It seems the question below had some questions about the lambda as well but I don't see that it answers my question.</p>
  
  <p><a href="https://stackoverflow.com/questions/810208/passing-a-parameter-using-relaycommand-defined-in-the-viewmodel-from-josh-smith">Passing a parameter using RelayCommand defined in the ViewModel (from Josh Smith example)</a></p>