title: C# Collection using Generic types or Interface
body: <p>I have a set of classes (<code>MyClass1, MyClass2, MyClass3</code>) that implement an interface (<code>IMyClass</code>).</p>
  
  <p>I am interested in creating a method to fill and return a list (<code>Collection.Generics.Lists&lt;&gt;</code>) of objects that have common interface <code>MyClass</code><em><code>X</code></em> (<code>List&lt;IMyClass&gt;</code>). But how?</p>
  
  <p>Each class has a constructor <code>MyClass</code><em><code>X</code></em> which does something different. So I can not create a <em>BaseClass</em> and use a common constructor for all.</p>
  
  <p>By interface I can not do:</p>
  
  <pre><code>public List&lt;IMyClass&gt; Fill(string[] ItemsPassedByParam, IMyClass InstanceOfMyClassX)
      List MyList = new List&lt;IMyClass&gt;();
      foreach (item in ItemsPassedByParam)
      {
          MyList.Add (new IMyClass (item)); //can't do new from an Interface!!
      }
  }
  </code></pre>
  
  <p>Also I tried using generics type, TMyClass in the next example, but neither has worked for me (though I have not very deep notions about generic types):</p>
  
  <pre><code>public List&lt;TMyClass&gt; Fill(string[] ItemsPassedByParam, Type TypeOfMyClassX)
      List MyList = new List &lt;TMyClass&gt; ();
      foreach (item in ItemsPassedByParam)
      {
          MyList.Add (new TMyClass (item)); //can't do new from a Generic Type!!
      }
  }
  </code></pre>
  
  <p>I tried also to externalize the code inside the Constructor of <code>MyClass</code><em><code>X</code></em> in a method, but I can't call the method since de object is not initialized (there is no <code>new ()</code> ).</p>
  
  <p>In these cases which is the solution?</p>
  
  <p>Thanks in advance!</p>