Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ObservableCollection<> vs. List<>

Tags:

c#

.net

wpf

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.

like image 426
Andriy Kizym Avatar asked Nov 16 '10 17:11

Andriy Kizym


People also ask

How do I cast ObservableCollection to list?

Given that ObservableCollection<T> implements IEnumerable<T> you can give it to the constructor of List<T> : List<T> myList = new List<T>(myObservableCollection); Where T is the type of the items in the collection.

What is ObservableCollection in C#?

An ObservableCollection is a dynamic collection of objects of a given type. Objects can be added, removed or be updated with an automatic notification of actions. When an object is added to or removed from an observable collection, the UI is automatically updated.

What is ObservableCollection in xamarin forms?

If you want the ListView to automatically update as items are added, removed and changed in the underlying list, you'll need to use an ObservableCollection . ObservableCollection is defined in System.Collections.ObjectModel and is just like List , except that it can notify ListView of any changes: C# Copy.


2 Answers

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

like image 84
Stan R. Avatar answered Sep 21 '22 08:09

Stan R.


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.

like image 22
Ian Griffiths Avatar answered Sep 20 '22 08:09

Ian Griffiths