title: When is it important to have a public parameterless constructor in C#?
body: <p>I'm trying to understand the <a href="http://msdn.microsoft.com/en-us/library/d5x73970.aspx" rel="nofollow noreferrer">constraints</a> on generic type parameters in C#.  What is the purpose of the <code>where T : new()</code> constraint?  Why would you need to insist that the type argument have a public parameterless constructor?</p>
  
  <p><strong>Edit:</strong>
  I must be missing something.  The highest rated answer says the public parameterless constructor is necessary to instantiate the generic type.  If that's the case, why the does this code compile and run?</p>
  
  <pre><code>namespace WindowsFormsApplication1
  {
      public partial class Form1 : Form
      {
          public Form1()
          {
              InitializeComponent();
          }
  
          private void Form1_Load(object sender, EventArgs e)
          {
              //class Foo has no public parameterless constructor
              var test = new genericClass&lt;Foo&gt;(); 
          }
      }
  
      class genericClass&lt;T&gt; where T : new()
      {
          T test = new T();  //yet no problem instantiating
      }
  
      class Foo
      {
          //no public parameterless constructor here
      }
  }
  </code></pre>
  
  <p><strong>Edit:</strong> In his comment, gabe reminded me that if I don't define a constructor, the compiler provides a parameterless one by default.  So, class Foo in my example actually does have a public parameterless constructor.</p>