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.
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.
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.
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.
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
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.
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