title: How can I pass an event to a function in C#?
body: <p>I am looking to pass an event to a helper function.  This function will attach a method to the event.  However, I am having trouble properly passing the event.  I have tried passing a <code>EventHandler&lt;TEventArgs&gt;</code>.  It compiles, but events are not attached (but are still added; it seems a copy of the event handler is made).</p>
  
  <p>For example, if I have this:</p>
  
  <pre><code>public event EventHandler&lt;EventArgs&gt; MyEvent;
  </code></pre>
  
  <p>And the helper function:</p>
  
  <pre><code>public static void MyHelperFunction&lt;TEventArgs&gt;(EventHandler&lt;TEventArgs&gt; eventToAttachTo)
  {
      eventToAttachTo += (sender, e) =&gt; { Console.WriteLine("Hello world"); };
  }
  </code></pre>
  
  <p>And the caller:</p>
  
  <pre><code>MyHelperFunction(MyEvent);
  MyEvent(null, new EventArgs()); // Does nothing.
  </code></pre>