title: Generic Constraint for Non Nullable types
body: <p>I have the following class:</p>
  
  <pre><code>public class KeyDTO&lt;T&gt;
  {
       public T Id { get; set; }
  }
  </code></pre>
  
  <p>So far so good, but I want the type parameter <strong>T</strong> to be a non-nullable type.
  I've read somewhere that this may be feasible:</p>
  
  <pre><code>public class KeyDTO&lt;T&gt; where T : IComparable, IComparable&lt;T&gt;
  {
       public T Id { get; set; }
  }
  </code></pre>
  
  <p>But, If i change <code>public T Id</code> to <code>public T? Id</code>, I get a compilation error telling me that <code>T</code> must be non-nullable.</p>
  
  <p>How can I specify that a generic type parameter must be non-nullable?</p>
  
  <h1>Edit</h1>
  
  <p>I want to accomplish this because I want to annotate my <code>Id</code> property with the <code>[Required]</code> attribute as follows:</p>
  
  <pre><code>public class KeyDTO&lt;T&gt; {
      [Required]
      public T Id { get; set; }
  }
  </code></pre>
  
  <p>What <code>[Required]</code> does is validate the model so <code>T</code> cannot be null.</p>
  
  <p>However, if I have <code>KeyDTO&lt;int&gt;</code>, <code>Id</code> will be initialized to <code>0</code>, bypassing my <code>[Required]</code> attribute</p>