Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 363
Matthias Meid Avatar asked May 14 '26 04:05

Matthias Meid


1 Answers

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.

like image 142
David Avatar answered May 16 '26 16:05

David