Andriy Kizym
Andriy Kizym

Reputation: 1796

ObservableCollection<> vs. List<>

I have lots of entities with nested List<> in each.

For example, I have BaseEntity which has List<ColumnEntity>. ColumnEntity class has List<Info> and so on.

We are working with a WPF UI, and we need to track all changes in every List of BaseEntity. It is implemented by instantiating a new ObservableCollection based on the needed list, and with binding to that ObservableCollection.

What are the pros and cons changing all these nested Lists to ObservableCollections? So we can track all changes in BaseEntity itself without reassigning each list of BaseEntity to modified bound ObservableCollection?

Assuming that methods specific to List are never used.

Upvotes: 87

Views: 113363

Answers (5)

Stan R.
Stan R.

Reputation: 16065

Interesting question, considering that both List and ObservableCollection implement IList<T> there isn't much of a difference there, ObservableCollection also implements INotifyCollectionChanged interface, which allows WPF to bind to it.

One of the main differences is that ObservableCollection does not have AddRange method, which might have some implications.

Also, I would not use ObservableCollection for places where I know I would not be binding to, for this reason, it is important to go over your design and make sure that you are taking the correct approach in separating layers of concern.

As far as the differences between Collection<T> and List<T> you can have a look here Generic Lists vs Collection

Upvotes: 105

iaminvinicble
iaminvinicble

Reputation: 430

One more important difference is you can access ObservableCollection only from thread on which it was created where as list can be accessed fromany thread.

Upvotes: 12

Rayan Elmakki
Rayan Elmakki

Reputation: 1194

List represents a strongly typed list of objects that can be accessed by index. It provides methods to search, sort, and manipulate lists. The List class is the generic equivalent of the ArrayList class. It implements the IList generic interface using an array whose size is dynamically increased as required.

ObservableCollection is a generic dynamic data collection that uses an interface "INotifyCollectionChanged" to provide notifications when items get added, removed, or when the whole collection is refreshed.

Read more about it in this link: http://www.codeproject.com/Articles/42536/List-vs-ObservableCollection-vs-INotifyPropertyCha

Upvotes: 13

codymanix
codymanix

Reputation: 29540

I see no problem with that, other than a very marginal performance overhead.

Note that if you modify the internal Lists directly, you are not notified about changes. Also if the objects which are contained in the ObservableCollection are modified you are not notified. Notification occurs only, if elements are added, replaced, removed or moved.

Upvotes: 7

Ian Griffiths
Ian Griffiths

Reputation: 14567

It depends on exactly what you mean by this:

we need to track all changes in every List of BaseEntity

Would it be enough to track changes to objects already in the list? Or do you need to know when objects are removed from/are added to/change positions within the list?

If a list will contain the same items for their whole lifetime, but the individual objects within that list will change, then it's enough for just the objects to raise change notifications (typically through INotifyPropertyChanged) and List<T> is sufficient. But if the list will contain different objects from time to time, or if the order changes, then you should use ObservableCollection<T>.

So while the differences may be interesting (and a previous poster has already covered those), typically you won't have that much of a choice - either you need ObservableCollection<T> or you don't.

Upvotes: 44

Related Questions