ObservableCollection<>对比列表<> [英] ObservableCollection<> vs. List<>
问题描述
我有很多实体,每个实体都带有嵌套的 List<>
.
I have lots of entities with nested List<>
in each.
例如,我有 BaseEntity
,它有 List
.ColumnEntity
类有 List
等.
For example, I have BaseEntity
which has List<ColumnEntity>
.
ColumnEntity
class has List<Info>
and so on.
我们正在使用 WPF UI,我们需要跟踪每个列表中的所有更改BaseEntity
.它是通过基于所需列表实例化一个 new ObservableCollection
并绑定到该 ObservableCollection
来实现的.
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
.
将所有这些嵌套的 Lists
更改为 ObservableCollections
的利弊是什么?所以我们可以跟踪 BaseEntity
本身的所有变化,而无需将 BaseEntity
的每个列表重新分配给修改后的绑定 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
?
假设从不使用特定于 List
的方法.
Assuming that methods specific to List
are never used.
推荐答案
有趣的问题,考虑到 List 和 ObservableCollection
都实现了 IList
没有太多有一点不同,ObservableCollection
还实现了 INotifyCollectionChanged
接口,它允许 WPF 绑定到它.
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.
主要区别之一是 ObservableCollection
没有 AddRange
方法,这可能会产生一些影响.
One of the main differences is that ObservableCollection
does not have AddRange
method, which might have some implications.
此外,我不会将 ObservableCollection
用于我知道我不会绑定到的地方,因此重要的是检查您的设计并确保您采用正确的方法分离关注的层.
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 making sure that you are taking the correct approach in separating layers of concern.
至于 Collection
和 List
之间的区别你可以看看这里通用列表与集合
As far as the differences between Collection<T>
and List<T>
you can have a look here
Generic Lists vs Collection
这篇关于ObservableCollection<>对比列表<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!