BindingList with IList<T> DataSource, Element Order
Can I assume that the order of the elements from an IList<T>
remains equal, when I use it as a DataSource
of a BindingSource
(Windows Forms), so that BindingSource.Position
and List.IndexOf()
are equivalent on the same object?
This is importan开发者_运维问答t to me, as Find
is not supported on lists that are no BindingList
, so I use the plain old IList.IndexOf
method to determine the position of an element:
myBindingSource.Position = myItemList.IndexOf(myItem);
Apparently this works fine. But I don't know whether I could not figure out yet whether I can actually depend on this to work. I'm a bit worried, because the BindingList
's List
property copies the data into new list structures (see the DataSource
remarks)...
Cheers, Matthias
Yes, IList<T>
should keep the same ordering at all times. It's a step beyond IEnumerable<T>
(in terms of additional functionality) which does not guarantee ordering, just the ability to enumerate over the collection. IList<T>
adds indexing to support more functionality such as .IndexOf()
and .RemoveAt()
which requires indexing.
Now, whether or not the data source from which you populate your IList<T>
returns items in the same order, that's another story.
精彩评论