title: C# Using foreach to iterate through a list containing 2 different object classes
body: <p>Here's my problem.</p>
  
  <p>I've got 2 classes that both inherit from a common class. I.E a car class and a van class which both inherit from a vehicle class. I added them to a common list of type vehicle. The car class has a noofseats attribute and the van has a weight attribute. Is there a way to iterate through them using a foreach loop and output based on whether they are a car class or van class?</p>
  
  <pre><code>foreach (van v in vehiclelist)
  {
    lsttest.Items.Add(v.weight;)
  }
  
  foreach (car c in vehiclelist)
  {
    lsttest.Items.Add(c.nofoseats;)
  }
  </code></pre>
  
  <p>I get an error of: 'Unable to cast object of type 'Test.Car' to type 'Test.Van'.'</p>
  
  <p>Is there an easy way to fix this or is it just not possible? Thanks</p>