title: Generic method overload ambiguous with nullable types
body: <p>Say I have two generic, overloaded methods of the form:</p>
  
  <pre><code>public string Do&lt;T&gt;(T maybeValue, Func&lt;T, string&gt; func)
    where T : class
  {
    if(maybeValue == null) return null;
    return func(maybeValue);
  }
  
  public string Do&lt;T&gt;(T? maybeValue, Func&lt;T, string&gt; func)
    where T : struct
  {
    if(!maybeValue.HasValue) return null;
    return func(maybeValue.Value);
  }
  </code></pre>
  
  <p>Now, calling the method with a variable of type nullable, C# refuses to compile and says that the call is ambiguous between the two overloads:</p>
  
  <pre><code>int? maybeX = 3;
  Do(maybeX, x =&gt; x.ToString());
  </code></pre>
  
  <blockquote>
    <p>The call is ambiguous between the following methods or properties:
    '<code>Program.Do&lt;int?&gt;(int?, System.Func&lt;int?,string&gt;)</code>' and
    '<code>Program.Do&lt;int&gt;(int?, System.Func&lt;int,string&gt;)</code>'</p>
  </blockquote>
  
  <p>Easy fixes are to include the generic parameter when calling the method, or to specify the type of the lambda argument:</p>
  
  <pre><code>Do&lt;int&gt;(maybeX, x =&gt; x.ToString());
  Do(maybeX, (int x) =&gt; x.ToString());
  </code></pre>
  
  <p>Interestingely, choosing <code>int?</code> as generic type during invocation will not compile</p>
  
  <blockquote>
    <p>The type must be a reference type in order to use it as parameter 'T' in the generic type or method".</p>
  </blockquote>
  
  <p>How come? Obviously only one of the two overloads can be used with a value of type <code>int?</code>, yet the compiler says the call is ambiguous. Can I further constrain the methods to help the compiler decide which method to call, without having the invoking code specify the type explicitely?</p>