title: List<T> Casting algorithms and performance considerations
body: <p>I have the following code</p>
  
  <pre><code>class Program
      {
          static void Main(string[] args)
          {
              List&lt;A&gt; aList = new List&lt;A&gt;();
  
              var aObj = new A();
  
              aObj.Go(aList.Cast&lt;IB&gt;());
          }
      }
  
      class A : IB
      {
          public void Go(IEnumerable&lt;IB&gt; interfaceList)
          {
              foreach (IB ibby in interfaceList)
              {
                  Console.WriteLine("Here");
              }
          }
      }
  
      interface IB
      {
          void Go(IEnumerable&lt;IB&gt; interfaceList);
      }
  </code></pre>
  
  <p>}</p>
  
  <p>I originally tried passing a List but that <a href="http://blogs.msdn.com/ericlippert/archive/2007/10/17/covariance-and-contravariance-in-c-part-two-array-covariance.aspx" rel="nofollow noreferrer">doesn't work</a>.
  After a lot of help from <a href="https://stackoverflow.com/questions/504729/how-do-i-cast-a-listt-effectively">SO</a> I found that passing IEnumerable is the only way to get the objects across as .ofType(IB).</p>
  
  <p>Unfortunately for me, in my code the following line will be executed thousands of times:</p>
  
  <pre><code>aList.Cast&lt;IB&gt;();
  </code></pre>
  
  <p>I was wondering if anyone knew how it was implemented algorithmically (in IL) and what its time order is.</p>
  
  <p>That is to say, is it faster than a foreach loop that just casts each item, or is that exactly what it does?</p>
  
  <p><strong>EDIT</strong> The main class needs to maintain a list of the actual objects. But the reader is only allowed to touch them through the interface. </p>