为什么ListBox.ObjectCollection和ListView.ListViewItemCollection有AddRange但没有InsertRange或RemoveRange?
因此,ListBox.ObjectCollection
和 ListView.ListViewItemCollection
都实现了 IList
类,该类提供了 Add
和 删除
方法,但不删除 AddRange
、InsertRange
或 RemoveRange
。但是,ListBox.ObjectCollection
和 ListView.ListViewItemCollection
还提供了 AddRange
方法 - 只是没有 InsertRange
或 >删除范围
。
另一方面,看看 ArrayList
类,它除了实现 IList
之外,还提供了 AddRange
、InsertRange
和删除范围
。此类和接口的通用形式 List
(具有 AddRange
、InsertRange
和 RemoveRange
)和IList
(仅提供Add
和Remove
)。
我可以理解 IList
和 IList
接口不提供 AddRange
等 - 它只是一个接口;超出 Add
、Remove
、RemoveAt
等最低要求的任何内容都是可选的。但是,考虑到 *Range
方法对于 ArrayList 和 List
类的有用性,并考虑到它们对于ListBox
和 ListView
控件,我想知道为什么这些控件不存在它们。
有谁知道吗? InsertRange
和 RemoveRange
的内部实现是否存在某些问题,导致这些方法在某种程度上效率较低、更加复杂,或者不太适合 ListBox.ObjectCollection
和 ListView.ListViewItemCollection
比 AddRange
?
需要明确的是:我并不是在寻找“看起来微软的某个人很懒”的猜测“ 种类;相反,我想知道是否有人真正知道 AddRange
和 InsertRange
/RemoveRange
之间的合法差异,这可能解释 ListBox.ObjectCollection
和 ListView.ListViewItemCollection
中缺少后面这些方法。
So, both ListBox.ObjectCollection
and ListView.ListViewItemCollection
implement the IList
class, which provides Add
and Remove
methods but no AddRange
, InsertRange
, or RemoveRange
. However, ListBox.ObjectCollection
and ListView.ListViewItemCollection
also provide an AddRange
method -- just no InsertRange
or RemoveRange
.
Look at the ArrayList
class, on the other hand, which on top of implementing IList
also provides AddRange
, InsertRange
, and RemoveRange
. The same difference exists between the generic forms of this class and interface, List<T>
(which has AddRange
, InsertRange
, and RemoveRange
) and IList<T>
(which only provides Add
and Remove
).
I can understand the IList
and IList<T>
interfaces not providing AddRange
, etc. -- it's just an interface; anything beyond the minimum requirements of Add
, Remove
, RemoveAt
, etc. is optional. But, given the usefulness of the *Range
methods for the ArrayList
and List<T>
classes, and considering how handy they'd be for ListBox
and ListView
controls, I wonder why they don't exist for these controls.
Does anyone know? Is there something about the internal implementation of InsertRange
and RemoveRange
that makes these methods somehow less efficient, more complicated, or otherwise less appropriate for ListBox.ObjectCollection
and ListView.ListViewItemCollection
than AddRange
?
To be clear: I'm not looking for speculation of the "Looks like someone at Microsoft got lazy" variety; rather, I'm wondering if anyone actually knows of a legitimate difference between AddRange
and InsertRange
/RemoveRange
that might explain the absence of these latter methods from ListBox.ObjectCollection
and ListView.ListViewItemCollection
.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一般来说,使用 InsertRange 和(更是如此)RemoveRange 的情况非常罕见。例如,RemoveRange 是否应该仅在存在连续范围内与给定范围相似的元素时删除给定范围,还是应该逐个删除给定元素?
如果您需要的话,这些方法通常很容易实现。在扩展方法的帮助下,您可以让它们看起来几乎像本机方法。
代码中没有任何迹象表明实现这些方法会很复杂(就像数千个其他“有用”的方法一样)。实际上,
InsertItems
的ListView
实现甚至采用了已经在引擎盖下的项目数组。标准ListViewItemCollection.Insert()
始终提供包含 1 个元素的数组。使用 Reflector 看一下ListViewNativeItemCollection.Insert()
:我想实现该功能并不那么重要......所以他们没有。
正如埃里克·利珀特 喜欢说:“因为没有人设计、指定、实现、测试、记录和交付该功能”。
In general it's a pretty uncommon scenario to use InsertRange and (even more so) RemoveRange. Should RemoveRange, for example, remove the given range only if there is a continious range of elements similar to the given range, or should it remove the given elements one by one?
These methods are usually easy to implement if you need them. With the help of the extension methods you can have them to look almost like the native methods.
There are no signs in the code, showing that it would be complicated to implement those methods (just as thousands of other "useful" methods). Actually, the
ListView
implementation ofInsertItems
even takes an array of items already under the hood. Just the standardListViewItemCollection.Insert()
always provides array with 1 element. Take a look with the Reflector to theListViewNativeItemCollection.Insert()
:I guess it just wasn't that important to implement that feature... so they didn't.
As Eric Lippert likes to say: "because no one ever designed, specified, implemented, tested, documented and shipped that feature".