title: C# Factory for generic inheritance
body: <p>I apologize for this rather fundamental question, however, I could find no documentation. Perhaps because I do not know the proper terminology.</p>
  
  <p>Class structure:</p>
  
  <pre><code>class D{}
  
  abstract class A&lt;T&gt;{}
  class B&lt;T&gt; : A&lt;T&gt; {}
  class C : B&lt;D&gt; {}
  </code></pre>
  
  <p>I attempt to create a factory method returning C, while the overall return type must be A. Unfortunately this implementation will produce a compiletime error, altough the inheritance structure seems to be consummately covariant.</p>
  
  <pre><code>public A&lt;T&gt; FactoryMethod()
  {
     return new C();
  }
  </code></pre>
  
  <p>More specifically, I am trying to implement a factory, able to produce all three classes based on a input value,  while the generic capability has to be obtained.</p>
  
  <pre><code>public A&lt;T&gt; FactoryMethod(int i, Type K)
  {
     if(i == 1)
        return new A&lt;K&gt;():
  
     if(i == 2)
        return new B&lt;K&gt;():
  
     if(i == 3)
        return new C():
  }
  </code></pre>
  
  <p><strong>Update</strong></p>
  
  <p>I have to create three objects as follows.</p>
  
  <pre><code>A&lt;string&gt; first = FactoryMethod(1, string);
  A&lt;int&gt; second = FactoryMethod(2, int);
  A&lt;int&gt; third = FactoryMethod(3, int);
  </code></pre>