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?
Building a new collection from an IEnumerable
as you did is the way to do it.
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));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With