为什么ListBox.ObjectCollection和ListView.ListViewItemCollection有AddRange但没有InsertRange或RemoveRange?

发布于 2024-08-05 13:26:09 字数 1560 浏览 2 评论 0原文

因此,ListBox.ObjectCollectionListView.ListViewItemCollection 都实现了 IList 类,该类提供了 Add删除 方法,但不删除 AddRangeInsertRangeRemoveRange。但是,ListBox.ObjectCollectionListView.ListViewItemCollection 还提供了 AddRange 方法 - 只是没有 InsertRange>删除范围

另一方面,看看 ArrayList 类,它除了实现 IList 之外,还提供了 AddRangeInsertRange删除范围。此类和接口的通用形式 List(具有 AddRangeInsertRangeRemoveRange)和IList(仅提供AddRemove)。

我可以理解 IListIList 接口不提供 AddRange 等 - 它只是一个接口;超出 AddRemoveRemoveAt 等最低要求的任何内容都是可选的。但是,考虑到 *Range 方法对于 ArrayList 和 List 类的有用性,并考虑到它们对于ListBoxListView 控件,我想知道为什么这些控件不存在它们。

有谁知道吗? InsertRangeRemoveRange 的内部实现是否存在某些问题,导致这些方法在某种程度上效率较低、更加复杂,或者不太适合 ListBox.ObjectCollectionListView.ListViewItemCollectionAddRange

需要明确的是:我并不是在寻找“看起来微软的某个人很懒”的猜测“ 种类;相反,我想知道是否有人真正知道 AddRangeInsertRange/RemoveRange 之间的合法差异,这可能解释 ListBox.ObjectCollectionListView.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

过去的过去 2024-08-12 13:26:09
  1. 一般来说,使用 InsertRange 和(更是如此)RemoveRange 的情况非常罕见。例如,RemoveRange 是否应该仅在存在连续范围内与给定范围相似的元素时删除给定范围,还是应该逐个删除给定元素?

  2. 如果您需要的话,这些方法通常很容易实现。在扩展方法的帮助下,您可以让它们看起来几乎像本机方法。

  3. 代码中没有任何迹象表明实现这些方法会很复杂(就像数千个其他“有用”的方法一样)。实际上,InsertItemsListView 实现甚至采用了已经在引擎盖下的项目数组。标准 ListViewItemCollection.Insert() 始终提供包含 1 个元素的数组。使用 Reflector 看一下 ListViewNativeItemCollection.Insert()

     public ListViewItem Insert(int index, ListViewItem item)
     {
         //排除各种检查....
         this.owner.InsertItems(index, new ListViewItem[] { item }, true);
         //更多代码排除......
         退货;
     }
    

我想实现该功能并不那么重要......所以他们没有。

正如埃里克·利珀特 喜欢说:“因为没有人设计、指定、实现、测试、记录和交付该功能”。

  1. 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?

  2. 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.

  3. 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 of InsertItems even takes an array of items already under the hood. Just the standard ListViewItemCollection.Insert() always provides array with 1 element. Take a look with the Reflector to the ListViewNativeItemCollection.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 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".

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文