title: Downcasting a list of objects in C#
body: <p>How can I downcast a list of objects so that each of the objects in the list is downcast to an object of a derived class?</p>
  
  <p>This is the scenario.</p>
  
  <p>I have a base class with a <code>List</code> of base items, and two classes inheriting from it:</p>
  
  <pre><code>public class BaseClass
  {
      public List&lt;BaseItem&gt; items;
      protected BaseClass()
      {
          // some code to build list of items
      }
  }
  public class DerivedClass : BaseClass
  {
      public DerivedClass : base() {}
  }
  public class AnotherDerivedClass : BaseClass
  {
      public AnotherDerivedClass : base() {}
  }
  
  public class BaseItem {}
  public class DerivedItem : BaseItem {}
  public class AnotherDerivedItem : BaseItem {}
  </code></pre>
  
  <p>The idea is to not have to duplicate the code needed to build the list of items. The <code>BaseItem</code> has all the basic stuff I need, and I can always downcast <code>BaseItem</code> to one of the derived items.</p>
  
  <p>The problem arises when I have a list of them. The <code>List</code> of <code>BaseItem</code> is declared in the <code>BaseClass</code> because all the derived classes have to have it. But when accessing it at runtime I can't seem to be able to downcast to the derived class.</p>