title: Nullable type as a generic parameter possible?
body: <p>I want to do something like this :</p>
  
  <pre><code>myYear = record.GetValueOrNull&lt;int?&gt;("myYear"),
  </code></pre>
  
  <p>Notice the nullable type as the generic parameter. </p>
  
  <p>Since the <code>GetValueOrNull</code> function could return null my first attempt was this: </p>
  
  <pre><code>public static T GetValueOrNull&lt;T&gt;(this DbDataRecord reader, string columnName)
    where T : class
  {
      object columnValue = reader[columnName];
  
      if (!(columnValue is DBNull))
      {
          return (T)columnValue;
      }
      return null;
  }
  </code></pre>
  
  <p>But the error I'm getting now is:</p>
  
  <blockquote>
    <p>The type 'int?' must be a reference type in order to use it as parameter 'T' in the generic type or method</p>
  </blockquote>
  
  <p>Right! <code>Nullable&lt;int&gt;</code> is a <code>struct</code>! So I tried changing the class constraint to a <code>struct</code> constraint (and as a side effect can't return <code>null</code> any more):</p>
  
  <pre><code>public static T GetValueOrNull&lt;T&gt;(this DbDataRecord reader, string columnName)
    where T : struct
  </code></pre>
  
  <p>Now the assignment:</p>
  
  <pre><code>myYear = record.GetValueOrNull&lt;int?&gt;("myYear");
  </code></pre>
  
  <p>Gives the following error:</p>
  
  <blockquote>
    <p>The type 'int?' must be a non-nullable value type in order to use it as parameter 'T' in the generic type or method</p>
  </blockquote>
  
  <p>Is specifying a nullable type as a generic parameter at all possible?</p>