ObservableCollection<> vs. List<>

C#.NetWpf

C# Problem Overview


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.

C# Solutions


Solution 1 - C#

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

Solution 2 - C#

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.

Solution 3 - C#

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

Solution 4 - C#

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.

Solution 5 - C#

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAndriy KizymView Question on Stackoverflow
Solution 1 - C#Stan R.View Answer on Stackoverflow
Solution 2 - C#Ian GriffithsView Answer on Stackoverflow
Solution 3 - C#Rayan ElmakkiView Answer on Stackoverflow
Solution 4 - C#iaminvinicbleView Answer on Stackoverflow
Solution 5 - C#codymanixView Answer on Stackoverflow