title: C# Create New T()
body: <p>You can see what I'm trying (but failing) to do with the following code:</p>
  
  <pre><code>protected T GetObject()
  {
      return new T();
  }
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>
  
  <p>EDIT:</p>
  
  <p>The context was as follows.  I was playing around with a custom controller class for all controllers to derive from, with standardised methods.  So in context, I needed to create a new instance of the object of the controller type.  So at time of writing, it was something like:</p>
  
  <pre><code>public class GenericController&lt;T&gt; : Controller
  {
      ...
  
      protected T GetObject()
      {
          return (T)Activator.CreateInstance(ObjectType);
      }        
  
      public ActionResult Create()
      {
          var obj = GetObject()
  
          return View(obj);
      }
  </code></pre>
  
  <p>And so I decided reflection was easiest here.  I agree that, certainly given the initial statement of the question, the most appropriate answer to mark as correct was the one using the new() constraint.  I have fixed that up.</p>