Why does ListBox.ObjectCollection and ListView.ListViewItemCollection have AddRange but not InsertRange or RemoveRange?
So both ListBox.ObjectCollection
and ListView.ListViewItemCollection
implement a class IList
that provides methods Add
and Remove
, but not AddRange
, InsertRange
or RemoveRange
. However, ListBox.ObjectCollection
and ListView.ListViewItemCollection
also provide a method AddRange
- simply not InsertRange
or RemoveRange
.
Take a look at a class ArrayList
that, in addition to implementation IList
, also provides AddRange
, InsertRange
and RemoveRange
. The same difference exists between the generic forms of this class and the interface, List<T>
(which has AddRange
, InsertRange
and RemoveRange
) and IList<T>
(which only provides Add
and Remove
).
I can understand interfaces IList
and IList<T>
without providing AddRange
etc. is just an interface; All that exceeds the minimum requirements Add
, Remove
, RemoveAt
etc., is optional. But given the usefulness of methods *Range
for classes ArrayList
and List<T>
and considering how convenient they are for controls ListBox
and ListView
, I wonder why they don't exist for those controls.
Somebody knows? Is there something about internal implementation InsertRange
and RemoveRange
what makes these methods less efficient, more complex, or less suitable for ListBox.ObjectCollection
and with ListView.ListViewItemCollection
what AddRange
?
To be clear: I'm not looking for the speculation "Looks like somebody at Microsoft has gotten lazy"; rather, I'm wondering if anyone knows of a legitimate distinction between AddRange
and InsertRange
/ RemoveRange
that might explain the lack of these latter methods from ListBox.ObjectCollection
and ListView.ListViewItemCollection
.
source to share
-
In general, a rather rare scenario for using InsertRange and (even more so) RemoveRange. If RemoveRange, for example, removes a given range only if there is a contiguous range of elements similar to the given range, or should it delete these elements one by one?
-
These methods are usually easy to implement if you need them. With extension methods, you can make them look almost like native methods.
-
There are no signs in the code to indicate that it would be difficult to implement these methods (as well as thousands of other "useful" methods). In fact, the implementation
ListView
InsertItems
even accepts an array of elements under the hood. Only the standardListViewItemCollection.Insert()
always provides an array with 1 element. Take a look with reflector atListViewNativeItemCollection.Insert()
:public ListViewItem Insert(int index, ListViewItem item) { //Various checks excluded .... this.owner.InsertItems(index, new ListViewItem[] { item }, true); //More code excluded ..... return item; }
I guess it wasn't that important to the implementation of this feature ... so they didn't.
As Eric Lippert likes to say , "because no one has ever designed, defined, implemented, tested, documented or posted this feature."
source to share