Convert array to ObservableCollection

The following array is used in a C # application:

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

However, I need projectArray as an ObservableCollection<CProject> . What would be the recommended way to do this? So far I have been using the following statement:

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

You can also use this expression or would you recommend other ways?

+8
arrays c # observablecollection
source share
2 answers

Creating a new collection from IEnumerable , as you did, is the way to do it.

+8
source share

IMHO, creating a new collection every time you want to add a range of objects is not very good, and I will follow the design below.

Create a class that inherits from ObservableCollection so that you can access the Items property that was protected, and then create an AddRange method that will add elements to 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)); } 
+1
source share

All Articles