title: c# convert ObservableCollection to another type
body: <p>I have the following problem that has got me stuck. In the below example I'm creating class A and class B and inheriting class A from class B. I then create an <code>ObservableCollection&lt;A&gt;</code> to hold a collection of people. What I then wish to do is expand class A to include the properties in class B and create this as a referenced copy ObservableCollection which I can update.</p>
  
  <p>The error I'm getting when this complies is:</p>
  
  <blockquote>
    <p>Unable to cast object of type
    <code>'&lt;CastIterator&gt;d__b1'1[iheriatance_obseravale_collection.Form1+B]'</code> to
    type
    <code>'System.Collections.ObjectModel.ObservableCollection'1[iheriatance_obseravale_collection.Form1+B]'</code>.</p>
  </blockquote>
  
  <pre><code>public class A : INotifyPropertyChanged
  {
      string _firstName;
      public string firstName
      {
          get
          {
              return _firstName;
          }
          set
          {
              if (_firstName != value)
              {
                  _firstName = value;
                  NotifyPropertyChanged("firstName");
              }
          }
      }
  
      string _surname;
      public string surname
      {
          get
          {
              return _surname;
          }
          set
          {
              if (_surname != value)
              {
                  _surname = value;
                  NotifyPropertyChanged("surname");
              }
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      public void NotifyPropertyChanged(string PropName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(PropName));
          }
      }
  }
  
  public class B : A, INotifyPropertyChanged
  {
      bool _isSelected;
      public bool isSelected
      {
          get
          {
              return _isSelected;
          }
          set
          {
              if (_isSelected != value)
              {
                  _isSelected = value;
                  NotifyPropertyChanged("isSelected");
              }
          }
      }
  
      public event PropertyChangedEventHandler PropertyChanged;
      public void NotifyPropertyChanged(string PropName)
      {
          if (PropertyChanged != null)
          {
              PropertyChanged(this, new PropertyChangedEventArgs(PropName));
          }
      }
  }
  
  ObservableCollection&lt;A&gt; peopleColl = new ObservableCollection&lt;A&gt;();
  
  private void addData()
  {
      peopleColl.Add(new A() { firstName = "James", surname = "Smith" });
      peopleColl.Add(new A() { firstName = "John", surname = "Woods" });
  }
  
  ObservableCollection&lt;B&gt; selectedPeople;
  
  private void selectAllPeople()
  {
      selectedPeople = (ObservableCollection&lt;B&gt;)peopleColl.Cast&lt;B&gt;();
  
      foreach (B person in selectedPeople)
      {
          person.isSelected = true;
          Console.WriteLine("Surname:{0}, IsSelected:{1}", person.surname, person.isSelected);
      }
  } 
  </code></pre>
  
  <p>Many thanks
  Stuart Turbefield</p>