wpf ObservableCollection 还是 IEnumerable?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13285299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
ObservableCollection or IEnumerable?
提问by Sylens
Which is faster ObservableCollection or IEnumerable to load a listbox?
哪个更快 ObservableCollection 或 IEnumerable 加载列表框?
I am currently using an IEnumerable as the ItemSource of the Listbox.
我目前使用 IEnumerable 作为列表框的 ItemSource。
Will it have any impact on the performance if I change this to ObservableCollection, if it is even possible?
如果我将其更改为 ObservableCollection(如果有可能),它会对性能产生任何影响吗?
回答by Stefán J?kull Sigurearson
That really depends on which class is implementing the IEnumerable. IEnumerable is simply an interface and any number of classes could be behind the actual implementation. You don't really bind the IEnumerable, since that is an interface, but you are binding some collection/list that implements IEnumerable. ObservableCollection also implements IEnumerable so should be able to safely use that as the ItemSource instead.
这实际上取决于哪个类正在实现 IEnumerable。IEnumerable 只是一个接口,在实际实现之后可以有任意数量的类。您并没有真正绑定 IEnumerable,因为它是一个接口,但是您正在绑定一些实现 IEnumerable 的集合/列表。ObservableCollection 还实现了 IEnumerable,因此应该能够安全地将其用作 ItemSource。
Enumerating through a IEnumerable to fill a ListBox should never be a performance bottleneck in your application, unless you would be using some custom class that does a lot of logic when iterating through values, in which case your design would probably be flawed.
通过 IEnumerable 枚举以填充 ListBox 永远不应成为应用程序中的性能瓶颈,除非您将使用某些自定义类在迭代值时执行大量逻辑,在这种情况下,您的设计可能会存在缺陷。
That being said, the best way is of course to simply measure. There is no substitute for measuring and making assumptions is always likely to come back to bite you later.
话虽如此,最好的方法当然是简单地测量。测量是无可替代的,做出假设总是有可能在以后反咬你一口。
回答by Cynical
I'm quite new to WPF and C#, but what I have understood so far is that if you use an IEnumerable as ItemSource, such as a List, you won't be able to see changes in that List unless you replace it with a different one:
我对 WPF 和 C# 还很陌生,但到目前为止我所了解的是,如果您使用 IEnumerable 作为 ItemSource,例如 List,您将无法看到该 List 中的更改,除非您将其替换为不同的一个:
IEnumerable<int> myList = new List<int>();
myList.Add(3);
In this case (provided that you do all the necessary stuff to inform your GUI that myListis changed) nothing will happen. If you do something like this, instead:
在这种情况下(假设您做了所有必要的事情来通知您的 GUImyList已更改)不会发生任何事情。如果你做这样的事情,而不是:
myList = new List<int> { 100, 200, 300 };
your GUI will be informed.
将通知您的 GUI。
If you use a ObservableCollection, instead, your GUI will be informed even when you add a new element with the Addmethod. So you have to decide whether you want to add elements to your ListBox or not.
相反,如果您使用 ObservableCollection,即使您使用该Add方法添加新元素,您的 GUI 也会收到通知。因此,您必须决定是否要向 ListBox 添加元素。
回答by Klaus78
If you bind to a collection that implements IEnumerableand that is not an ObservableCollection, for example a List, the binding target will not be notified of items added/removed.
如果您绑定到实现了IEnumerable并且不是 的集合,ObservableCollection例如 a List,则绑定目标将不会收到添加/删除项目的通知。
Not using a ObservableCollection does only make sense if the number of items in the collection does not change. Only then it makes sense to compare ObservableCollection with other objects. If the number of items change, then you do not have any choice but using an ObservableCollection.
不使用 ObservableCollection 只有在集合中的项目数量没有改变时才有意义。只有这样,将 ObservableCollection 与其他对象进行比较才有意义。如果项目数量发生变化,则您别无选择,只能使用 ObservableCollection。
In addition, as already pointed out by other users, you do not have to compare ObservableCollection with IEnumerable, but with a class implementing IEnumerable.
此外,正如其他用户已经指出的,您不必将 ObservableCollection 与 IEnumerable 进行比较,而是与实现 IEnumerable 的类进行比较。
As an example, a comparison ObservableCollection - List can be found in Stack Overflow question ObservableCollection<> vs. List<>.
例如,比较 ObservableCollection - List 可以在 Stack Overflow 问题ObservableCollection<> vs. List<> 中找到。

