title: How do you specify that a generic reference type is nullable when a subclass constraint is used in c# 8?
body: <p>How do you specify that a generic reference type is nullable when a subclass constraint is used in c# 8? I have done some searching and I've found no useful results.</p>
  
  <p>I know quite well that it is sufficient to add <code>where T: class</code> to specify that a generic type parameter <code>T</code> is a reference type. This way, the <code>nullable</code> reference annotations and warning features of C# 8 will work perfectly.</p>
  
  <p>However, there's the case where the generic type parameter <code>T</code> is a sub-type of another class—say <code>SomeClass</code>. Now, when you add <code>where T: SomeClass</code>, you are basically telling the compiler that <code>T</code> is a reference type which is a sub-type of <code>SomeClass</code>, since a value type cannot be a sub-type of a reference type. However, the compiler doesn't seem to understand this when it comes to the <code>nullable</code> reference annotations and warning features of C# 8. How do you make it clear to the compiler?</p>
  
  <p>Want to see some code? This is what I mean:</p>
  
  <pre class="lang-cs prettyprint-override"><code>// this compiles
  class ClassA&lt;T&gt; where T: class
  {
      public GenericType&lt;T?&gt; DoSomething()
      {
          thrown new NotImplementedException();
      }
  }
  </code></pre>
  
  <pre class="lang-cs prettyprint-override"><code>// this doesn't compile, but SomeClass is a class and it should work
  class ClassA&lt;T&gt; where T: SomeClass
  {
      // here, you get the classic:
      // Only non-nullable value type could be underlying of 'System.Nullable'
      // What this makes clear is that the compiler doesn't understand that
      // T is strictly a reference Type now
      public GenericType&lt;T?&gt; DoSomething()
      {
          thrown new NotImplementedException();
      }
  }
  </code></pre>