20

In a C# application the following array is used:

CProject[] projectArray = this.proxy.getProjectList(username, password);

However I need the projectArray as a ObservableCollection<CProject>. What would be the recommended way to do this? So far I'm using the following statement:

ObservableCollection<CProject> projectList = new ObservableCollection<CProject>(projectArray);

Would you also use this statement or would you recommend other ways?

Robert Strauch
  • 12,055
  • 24
  • 120
  • 192

2 Answers2

18

Building a new collection from an IEnumerable as you did is the way to do it.

ken2k
  • 48,145
  • 10
  • 116
  • 176
5

IMHO , Building a new collection each time you want to add range of objects is not good rather i would follow below design.

Build a class inheriting from ObservableCollection , so that you can access Items property which is protected and then create a AddRange Method which will add items into it

public class MyObObservableCollection<T> : ObservableCollection<T>
{
    public void AddRange(IEnumerable<T> items)
    {
        foreach (var item in items)
        {
            this.Items.Add(item);
        }

    }
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
}
TalentTuner
  • 17,262
  • 5
  • 38
  • 63