title: Is there a way to cast generic lists to lists of interface/base class types?
body: <p>I'm trying to show someone a use for interfaces in a crazy situation they've created.  They have several unrelated objects in lists, and need to perform an operation on two string properties in each object.  I'm pointing out that if they define the properties as part of an interface, they can use the interface object as the type for a method parameter that acts on it; for example:</p>
  
  <pre><code>void PrintProperties(IEnumerable&lt;ISpecialProperties&gt; list)
  {
      foreach (var item in list)
      {
          Console.WriteLine("{0} {1}", item.Prop1, item.Prop2);
      }
  }</code></pre>
  
  <p>This seems like it's all good, but the lists that need to be worked on aren't (and shouldn't) be declared with the interface as the type parameter.  However, it doesn't seem like you can cast to a different type parameter.  For example, this fails and I can't understand why:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  
  namespace ConsoleApplication2
  {
      class Program
      {
          static void Main(string[] args)
          {
              List&lt;Test&gt; myList = new List&lt;Test&gt;();
              for (int i = 0; i &lt; 5; i++)
              {
                  myList.Add(new Test());
              }
  
              PrintList((IEnumerable&lt;IDoSomething&gt;)myList);
          }
  
          static void PrintList(IEnumerable&lt;IDoSomething&gt; list)
          {
              foreach (IDoSomething item in list)
              {
                  item.DoSomething();
              }
          }
      }
  
      interface IDoSomething
      {
          void DoSomething();
      }
  
      public class Test : IDoSomething
      {
          public void DoSomething()
          {
              Console.WriteLine("Test did it!");
          }
      }
  }</code></pre>
  
  <p>I <em>can</em> use the <code>Enumerable.Cast&lt;T&gt;</code> member to do this, but I was looking for a method that might work in .NET 2.0 as well.  It seems like this should be possible; what am I missing? </p>