Xero Xero - 1 year ago 118
C# Question

ObservableCollection or IEnumerable?

Which is faster ObservableCollection or IEnumerable to load a listbox?

I am currently using an IEnumerable as the ItemSource of the Listbox.

Will it have any impact on the performance if I change this to ObservableCollection, if it is even possible?

Answer Source

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.

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.

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download