title: How C# Compiler choose SelectMany when translating LINQ expression?
body: <p>There are 4 overloaded signatures for Enumerable.SelectMany. To make it simple, we ignore the two signatures with <em>int</em> argument. So we have 2 signatures for SelectMany:</p>
  
  <pre><code>public static IEnumerable&lt;TResult&gt; SelectMany&lt;TSource, TResult&gt;(
      this IEnumerable&lt;TSource&gt; source,
      Func&lt;TSource, IEnumerable&lt;TResult&gt;&gt; selector
  )
  
  public static IEnumerable&lt;TResult&gt; SelectMany&lt;TSource, TCollection, TResult&gt;(
      this IEnumerable&lt;TSource&gt; source,
      Func&lt;TSource, IEnumerable&lt;TCollection&gt;&gt; collectionSelector,
      Func&lt;TSource, TCollection, TResult&gt; resultSelector
  )
  </code></pre>
  
  <p>My question is: how C# compiler choose SelectMany when translating LINQ expression to extension method invocation?</p>
  
  <p>Basically, if there are multiple <strong>from</strong> in LINQ expression, there will be SelectMany. But, it seems that C# compiler only choose the second signature. The first signature is never used.</p>
  
  <pre><code>        IEnumerable&lt;int&gt; en1 = Enumerable.Range(1, 3);
          IEnumerable&lt;double&gt; en2 = new double[] { 1.0, 3.14 };
  
          IEnumerable&lt;string&gt; en3 =
              from i1 in en1
              from i2 in en2
              select (i1 * i2).ToString();
  
          foreach (var i in en3)
          {
              Console.WriteLine(i);
          }
  </code></pre>
  
  <p>With the help of Reflector, I can see that above LINQ expression is translated into </p>
  
  <pre><code>en1.SelectMany&lt;int, double, string&gt;(delegate (int i1) {
          return en2;
      }, delegate (int i1, double i2) {
          double CS$0$0000 = i1 * i2return CS$0$0000.ToString();
      })
  </code></pre>
  
  <p>Above example involves 3 types. So, it is reasonable to select the second SelectMany signature. However, for below example, only one type is involved, it still selects the second signature.</p>
  
  <pre><code>        IEnumerable&lt;int&gt; en4 =
              from i1 in en1
              from i2 in Enumerable.Range(0, i1)
              select i2;
  </code></pre>
  
  <p>It is translated into:</p>
  
  <pre><code>en1.SelectMany&lt;int, int, int&gt;(delegate (int i1) {
          return Enumerable.Range(0, i1);
      }, delegate (int i1, int i2) {
          return i2;
      })
  </code></pre>
  
  <p>So, I cannot find a case that LINQ expression is translated into the first SelectMany signature. Is there such case?</p>
  
  <p>If the first SelectMany signature is not used, then it exists just because it is BIND of monad in functional programming?</p>
  
  <p>Perhaps the question can be: why do we have 2 signatures of SelectMany?</p>
  
  <p>Thanks.</p>