title: Delegate arguments causing ambiguous overloaded functions resolution
body: <p>I ran into an odd compilation issue while I was implementing a simple timing utility class.</p>
  
  <p>I implemented the following class that would allow wrapping calls to functions with timing related measurement information.</p>
  
  <pre><code>public static class TimeMeasument
  {
      public static void LogRunTime&lt;T&gt;(Action&lt;T&gt; func, T arg)
      {
          Stopwatch timer = Stopwatch.StartNew();
          try { func(arg); }
          finally { Console.WriteLine("Elapsed time {0} ms", timer.ElapsedMilliseconds); }
      }
  
      public static TResult LogRunTime&lt;TResult, T&gt;(Func&lt;T, TResult&gt; func, T arg)
      {
          Stopwatch timer = Stopwatch.StartNew();
          try { return func(arg); }
          finally { Console.WriteLine("Elapsed time {0} ms", timer.ElapsedMilliseconds); }
      }
  }
  </code></pre>
  
  <p>The problem I have is that when I went to use the function I received an <code>Ambiguous function call</code> compiler error when trying to call functions that have a return value. </p>
  
  <pre><code>// Example function
  string SayHello(string name)  { return "Hello " + name; }
  
  // Compiler error 
  // Ambiguous call between the following methods
  // LogRunTime&lt;T&gt;(Action&lt;T&gt; func, T arg)
  // and LogRunTime&lt;TResult, T&gt;(Func&lt;T,TResult&gt; func, T arg)
  //-----------------------------------------------------------------
  string x = TimeMeasument.LogRunTime(SayHello, "Joe");
  </code></pre>
  
  <p>I need to explicitly create a <code>Func&lt;T,U&gt;</code> for the function to remove the ambiguity.</p>
  
  <pre><code>// Compiles fine
  Func&lt;string, string&gt; func = SayHello;
  string s = TimeMeasument.LogRunTime(func, "Joe");
  </code></pre>
  
  <p>I was wondering why the <code>Action&lt;T&gt;</code> overload is being considered by the compiler as an option. I know that the generic type parameter for Action is satisfied by the second argument passed to <code>LogRunTime</code> but the function <code>SayHello</code> is not convertible to an Action. Doing so will result in the following error.</p>
  
  <pre><code>// Compiler error
  // SayHello(string) has the wrong return type
  //-------------------------------
  Action&lt;string&gt; action = SayHello;
  </code></pre>
  
  <p>Can anyone shed some light on why the <code>Action&lt;T&gt;</code> overload is considered as a valid target?</p>
  
  <p>Is there any other way of getting around this issue without explicitly creating/casting each function call?</p>