title: OOC: What is the difference between ToList() and casting to List<T> in .NET?
body: <p>OOC: Out Of Curiosity</p>
  
  <p>So, as a little exercise and for the sake of learning, I decided to check if I was able to implement a very basic recursive function that would return a <code>List&lt;int&gt;</code>, but with the following restrictions:</p>
  
  <p>1- The result should be returned by the function itself (as opposed to passed as an argument to a <code>void</code> function).</p>
  
  <p>2 - No local "named" variables declared in the function's body.</p>
  
  <p>I came up with the solution below (BTW: can this be improved in any way?)</p>
  
  <p>While doing this, I learnt that <code>ToList()</code> is not the same thing as casting to <code>List&lt;T&gt;</code> (see example below) - Anyone out there who can explain what happens under the hood and what the difference between the two is?</p>
  
  <p>Thanks!</p>
  
  <p>PS - I'm using version 4.0 (in case it matters).</p>
  
  <p>EDIT: the runtime error is <code>Unable to cast object of type '&lt;ConcatIterator&gt;d__71'1[System.Int32]' to type 'System.Collections.Generic.List'1[System.Int32]'</code></p>
  
  <pre><code>public static List&lt;int&gt; SomeIntegers(int min, int max)
  {
      //assume max &gt;= min for simplicity  
      if (min == max)
          return new List&lt;int&gt;() { min };
  
      // runtime error 
      //return (List&lt;int&gt;)(SomeIntegers(min, max - 1).Concat(new List&lt;int&gt;() { max }));   
  
      //works
      return (SomeIntegers(min, max - 1).Concat(new List&lt;int&gt;() { max })).ToList(); 
  }
  </code></pre>