title: Why doesn't C# infer my generic types?
body: <p>I'm having lots of Funcy fun (fun intended) with generic methods. In most cases C# type inference is smart enough to find out what generic arguments it must use on my generic methods, but now I've got a design where the C# compiler doesn't succeed, while I believe it could have succeeded in finding the correct types.</p>
  <p>Can anyone tell me whether the compiler is a bit dumb in this case, or is there a very clear reason why it can't infer my generic arguments?</p>
  <p>Here's the code:</p>
  <p>Classes and interface definitions:</p>
  <pre><code>interface IQuery&lt;TResult&gt; { }
  
  interface IQueryProcessor
  {
      TResult Process&lt;TQuery, TResult&gt;(TQuery query)
          where TQuery : IQuery&lt;TResult&gt;;
  }
  
  class SomeQuery : IQuery&lt;string&gt;
  {
  }
  </code></pre>
  <p>Some code that does not compile:</p>
  <pre><code>class Test
  {
      void Test(IQueryProcessor p)
      {
          var query = new SomeQuery();
  
          // Does not compile :-(
          p.Process(query);
  
          // Must explicitly write all arguments
          p.Process&lt;SomeQuery, string&gt;(query);
      }
  }
  </code></pre>
  <p>Why is this? What am I missing here?</p>
  <p>Here's the compiler error message (it doesn't leave much to our imagination):</p>
  <blockquote>
  <p>The type arguments for method IQueryProcessor.Process&lt;TQuery,
  TResult&gt;(TQuery) cannot be inferred from the usage. Try specifying the
  type arguments explicitly.</p>
  </blockquote>
  <p>The reason I believe C# should be able to infer it is because of the following:</p>
  <ol>
  <li>I supply an object that implements <code>IQuery&lt;TResult&gt;</code>.</li>
  <li>That only <code>IQuery&lt;TResult&gt;</code> version that type implements is <code>IQuery&lt;string&gt;</code> and thus TResult must be <code>string</code>.</li>
  <li>With this information the compiler has TResult and TQuery.</li>
  </ol>