title: C# compiler guessing data type wrong
body: <p>I've stumbled upon this (in my point of view error) weird behavior, and I would appreciate it if someone could tell me why the compiler behaves like this, or maybe I've got lucky and found a compiler bug ;-)</p>
  <p>All is compiled against .net6.0 (using VS 2022)</p>
  <p>This is the first version of the code:</p>
  <pre><code>internal class Program
  {
  
      private static void Main(string[] args)
      {
          long longValue = 0;
          string stringValue = &quot;&quot;;
          bool boolValue = false;
          DateTime dateTimeValue = DateTime.Now;
          double doubleValue = 0;
  
          Console.Write(&quot;expect long =&gt; &quot;);
          SetValue(val =&gt; longValue = val);
  
          Console.Write(&quot;expect string =&gt; &quot;);
          SetValue(val =&gt; stringValue = val);
  
          Console.Write(&quot;expect bool =&gt; &quot;);
          SetValue(val =&gt; boolValue = val);
  
          Console.Write(&quot;expect DateTime =&gt; &quot;);
          SetValue(val =&gt; dateTimeValue = val);
  
          Console.ReadKey();
      }
  
      private static void SetValue(Action&lt;long&gt; setter)
      {
          Console.WriteLine(&quot;long called&quot;);
          setter(1337);
      }
  
      private static void SetValue(Action&lt;string&gt; setter)
      {
          Console.WriteLine(&quot;string called&quot;);
          setter(&quot;foo&quot;);
      }
  
      private static void SetValue(Action&lt;bool&gt; setter)
      {
          Console.WriteLine(&quot;bool called&quot;);
          setter(true);
      }
  
      private static void SetValue(Action&lt;DateTime&gt; setter)
      {
          Console.WriteLine(&quot;DateTime called&quot;);
          setter(DateTime.Now.AddDays(10));
      }
  }
  </code></pre>
  <p>It produces this output:</p>
  <pre><code>expect long =&gt; long called
  expect string =&gt; string called
  expect bool =&gt; bool called
  expect DateTime =&gt; DateTime called
  </code></pre>
  <p>Now I add  a setValue call for doubleValue</p>
  <pre><code>        Console.Write(&quot;expect double =&gt; &quot;);
          SetValue(val =&gt; doubleValue = val);
  </code></pre>
  <p>Here I'm expecting a compiler error complaining there is no suitable overloaded version of SetValue. But instead, it compiles fine and produces this output:</p>
  <pre><code>expect long =&gt; long called
  expect string =&gt; string called
  expect bool =&gt; bool called
  expect DateTime =&gt; DateTime called
  expect double =&gt; long called
  </code></pre>
  <p>Notice the last line where we can see that the program has called the wrong setValue version (at this point, there is no &quot;right&quot; setValue version for the double data type).</p>
  <p>If I now add a SetValue version that handles doubles:</p>
  <pre><code>    private static void SetValue(Action&lt;double&gt; setter)
      {
          Console.WriteLine(&quot;double called&quot;);
          setter(13.37);
      }
  </code></pre>
  <p>the compiler complains <code>CS0121  The call is ambiguous between the following methods or properties: 'Program.SetValue(Action&lt;long&gt;)' and 'Program.SetValue(Action&lt;double&gt;)'</code></p>
  <p>I can now make the call unambiguous in several ways. First, as you can see in the final version of the code:</p>
  <pre><code>internal class Program
  {
  
      private static void Main(string[] args)
      {
          long longValue = 0;
          string stringValue = &quot;&quot;;
          bool boolValue = false;
          DateTime dateTimeValue = DateTime.Now;
          double doubleValue = 0;
  
          Console.Write(&quot;expect long =&gt; &quot;);
          SetValue(val =&gt; longValue = val);
  
          Console.Write(&quot;expect string =&gt; &quot;);
          SetValue(val =&gt; stringValue = val);
  
          Console.Write(&quot;expect bool =&gt; &quot;);
          SetValue(val =&gt; boolValue = val);
  
          Console.Write(&quot;expect DateTime =&gt; &quot;);
          SetValue(val =&gt; dateTimeValue = val);
  
          Console.Write(&quot;expect double =&gt; &quot;);
          SetValue(new Action&lt;double&gt;(val =&gt; doubleValue = val));
  
          Console.Write(&quot;expect double =&gt; &quot;);
          SetValue((double val) =&gt; doubleValue = val);
  
          Console.ReadKey();
      }
  
      private static void SetValue(Action&lt;long&gt; setter)
      {
          Console.WriteLine(&quot;long called&quot;);
          setter(1337);
      }
  
  
      private static void SetValue(Action&lt;string&gt; setter)
      {
          Console.WriteLine(&quot;string called&quot;);
          setter(&quot;foo&quot;);
      }
  
      private static void SetValue(Action&lt;bool&gt; setter)
      {
          Console.WriteLine(&quot;bool called&quot;);
          setter(true);
      }
  
      private static void SetValue(Action&lt;DateTime&gt; setter)
      {
          Console.WriteLine(&quot;DateTime called&quot;);
          setter(DateTime.Now.AddDays(10));
      }
  
      private static void SetValue(Action&lt;double&gt; setter)
      {
          Console.WriteLine(&quot;double called&quot;);
          setter(13.37);
      }
  }
  </code></pre>
  <p>Output:</p>
  <pre><code>expect long =&gt; long called
  expect string =&gt; string called
  expect bool =&gt; bool called
  expect DateTime =&gt; DateTime called
  expect double =&gt; double called
  expect double =&gt; double called
  </code></pre>
  <p>Why are these two versions (long, double) ambiguous for the compiler, and why is it still able to call the correct setValue version for the long variable but fails at the double value?</p>