title: Generic class with self-referencing type constraint
body: <p>Consider the following code:</p>
  
  <pre><code>abstract class Foo&lt;T&gt;
      where T : Foo&lt;T&gt;, new()
  {
      void Test()
      {
          if(Bar != null)
              Bar(this);
      }
  
      public event Bar&lt;T&gt; Bar;
  }
  
  delegate void Bar&lt;T&gt;(T foo)
      where T : Foo&lt;T&gt;, new();
  </code></pre>
  
  <p>The line <code>Bar(this)</code> results in the following compiler Error:<br>
  <em>Argument type Foo&lt;T&gt; is not assignable to parameter type T</em></p>
  
  <p>T is constrained to Foo&lt;T&gt; as I want derived classes to basically tell the base class their type, so that the type can be used in the event callback in order to save the implementor from having to cast the callback argument to the derived type.</p>
  
  <p>I can see the code doesn't quite work but I'm having a bit of a blockage as to how to do this correctly without ending up with a generic delegate that can be used for any old thing. I'm also not quite sure why the T constraint doesn't create a compiler error considering it seems to be recursive.</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I need to clarify this I think! Here's a new example which, I hope will be much clearer. Note below that the <code>OnDuckReady</code> event handler below generates a compiler error.</p>
  
  <p>How do I get the event to pass in the correct type?</p>
  
  <pre><code>abstract class Animal&lt;T&gt;
      where T : Animal&lt;T&gt;, new()
  {
      void Test()
      {
          if(AnimalReady != null)
              AnimalReady(this);
      }
  
      public event AnimalHandler&lt;T&gt; AnimalReady;
  }
  
  delegate void AnimalHandler&lt;T&gt;(Animal&lt;T&gt; animal)
      where T : Animal&lt;T&gt;, new();
  
  class Duck : Animal&lt;Duck&gt;
  {
      public void FlyAway()
      {
      }
  }
  
  class Test
  {
      void Main()
      {
          Duck duck = new Duck();
          duck.AnimalReady += OnDuckReady; // COMPILER ERROR
      }
  
      void OnDuckReady(Duck duck)
      {
          duck.FlyAway();
      }
  }
  </code></pre>