title: Passing Arguments to a Method in EventHandler
body: <p>This question has been asked many times but the solution is not working for some reason.</p>
  
  <p>I am dynamically creating a Button and assigning an EventHandler to it.</p>
  
  <pre><code>protected void Page_Load (object sender, EventArgs e)
  {
      Button b = new Button();
      b.Click += new EventHandler(Method);
  }
  
  protected void Method (object sender, EventArgs e)
  {
     //Do work here
  }
  </code></pre>
  
  <p>I needed to pass an argument so the most simple way I thought of was this:</p>
  
  <pre><code>b.Click += new EventHandler(Method(sender, e, "name"));
  
  protected void Method (object sender, EventArgs e, String name)
  {
     //Do work here
  }
  </code></pre>
  
  <blockquote>
    <p><strong>Error:</strong> Method name expected</p>
  </blockquote>
  
  <p>So after checking the questions here, I found the same solution in almost every question.</p>
  
  <pre><code>b.Click += new EventHandler((sender, e) =&gt; Method(sender, e, "name"));
  </code></pre>
  
  <blockquote>
    <p><strong>Error:</strong> A local variable named "sender" cannot be declared in this scope because it would give a different meaning to "sender", which is
    already used in a 'parent or current' scope to denote something else.</p>
  </blockquote>
  
  <p>So I changed to the following:</p>
  
  <pre><code>b.Click += new EventHandler((sender1, e1) =&gt; Method(sender1, e1, "name"));
  </code></pre>
  
  <p>And the error from Visual Studio was gone, but upon running the webpage, I received this error:</p>
  
  <blockquote>
    <p>System.ArgumentOutOfRangeException: Index was out of range. Must be a
    non-negative and less than the size of the collection.</p>
  </blockquote>
  
  <p>What's the problem here I'm really lost.</p>