Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

List or ObservableCollection?

Is there any difference in performance to use ObservableCollection, which (as I understand) refreshes view each time item added to collection, or instead use simple List collection, and refresh whole view when all elements already added?

And is the scenario with List as decried above even possible? How to implement this then?

I'm asking because my ItemsControl is slow, and I wonder if it would be better to use simple List instead ObservableCollection. I need to refresh the view only once at a time, when actually all elements already added to collection.

like image 827
Ksice Avatar asked May 13 '26 20:05

Ksice


1 Answers

ObservableCollection<T> implements IList<T> just as List<T> does. The main difference is of course, that it implements INotifyCollectionChanged which allows WPF to bind to it.

The ObservableCollection<T> throws an event after each change so the UI can Refresh. If you are adding a lot of Items sequentially, it can have some impact to your performance but that is unlikely. You can test this rather simple by using the Constructor which takes a List:

var originalList = new List<SomeClass>();

foreach ([..])
{
  originalList.Add(someInstance);
}

ObservableCollection<SomeClass> uiCollection = new ObservableCollection<SomeClass>(originalList);

This way you can create you complex List of objects and after its finished you can create an ObservableCollection out of it which you will Bind to on the UI.

like image 124
Ian Avatar answered May 16 '26 12:05

Ian