title: Instantiating a generic type?
body: <p>I'm currently working on a generic collection class and I'd like to create a method which returns an object from the collection.  If the specific object does not exist in the collection then the object should be created, added to the collection, and then returned.</p>
  
  <p>I'm encountering a few problems though.  The generic collection if of a type which represents an abstract class and I'm having trouble instantiating that.</p>
  
  <p>Here's my class definition:</p>
  
  <pre><code>public class CacheCollection&lt;T&gt; : List&lt;CacheItem&lt;T&gt;&gt; where T : EntityBase
  </code></pre>
  
  <p>And here's the partially complete method on which I am working:</p>
  
  <pre><code>public T Item(int Id)
  {
      CacheItem&lt;T&gt; result = this.Where(t =&gt; t.Entity.Id == Id).First();
  
      if (result == null) //item not yet in cache, load it!
      {
          T entity = new T(Id); //design time exception here: Cannot create an instance of the variable type 'T' because it does not have the new() constraint
          result = new CacheItem&lt;T&gt;(entity);
          this.Add(result);
      }
  
      return result.Entity;
  }
  </code></pre>
  
  <p>Any ideas on how to get around this?</p>
  
  <p>EDIT:  All classes derived from EntityBase have Id as a read-only property.</p>