wpf 为什么 ObservableCollection 没有 RemoveAll 方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11688865/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why ObservableCollection does not have a RemoveAll method?
提问by Shakti Prakash Singh
Why ObservableCollectiondoesn't has the RemoveAllmethod like a List?
为什么ObservableCollection没有RemoveAll像 a 这样的方法List?
I have implemented an extensionmethod to provide this functionality to the ObservableCollection, but I would like to understand if there is a specific reason for not providing this functionality.
我已经实现了一个扩展方法来向 提供此功能ObservableCollection,但我想了解是否有不提供此功能的特定原因。
Would it possibly effect Data Bindingin some way due to Collection change? Thispost does specify a few things that could go wrong while using ObservableCollections, but does not address this question.
由于集合更改,它是否可能以某种方式影响数据绑定?这篇文章确实指定了使用 时可能出错的一些事情,但没有解决这个问题。ObservableCollections
回答by Rachel
It does have a Clear()method that removes all items that you can use instead.
它确实有一种Clear()方法可以删除您可以使用的所有项目。
If I had to hazard a guess as to why they used Clearinstead of RemoveAll, I think it would be because RemoveAllcarries the suggestion that you are removing items from the collection, while Cleartells you the items are simply being cleared.
如果我不得不猜测他们为什么使用Clear而不是RemoveAll,我认为那是因为RemoveAll带有您正在从集合中删除项目的建议,同时Clear告诉您这些项目只是被清除。
This makes a difference in the type of CollectionChangednotification that gets raised. Clear()raises a NotifyCollectionChangedAction.Resetevent and does not include the removed items in the event, while Removeraises a NotifyCollectionChangedAction.Removedevent, and passes the removed item to the event.
这在CollectionChanged引发的通知类型上有所不同。Clear()引发NotifyCollectionChangedAction.Reset事件,不包括事件中移除的项目,而Remove引发NotifyCollectionChangedAction.Removed事件,并将移除的项目传递给事件。
You cannot raise a CollectionChangedevent with multiple items, so raising a NotifyCollectionChangedAction.Removedevent with all the items removed would throw an exception. The alternative would be to raise a CollectionChangedevent for every item that got removed, which can be quite bad for performance. And simply raising a NotifyCollectionChangedAction.Resetevent would cause some confusion when users are expecting a Removedevent to occur when they are removing items.
您不能引发CollectionChanged具有多个项目的事件,因此引发NotifyCollectionChangedAction.Removed删除所有项目的事件会引发异常。另一种方法是CollectionChanged为每个被删除的项目引发一个事件,这可能对性能非常不利。NotifyCollectionChangedAction.Reset当用户Removed在删除项目时期望发生事件时,简单地引发事件会引起一些混乱。
So I am guessing they decided to simply use .Clear()instead of .RemoveAll()because the name is a better description of what is actually happening behind the scenes.
所以我猜他们决定简单地使用.Clear()而不是.RemoveAll()因为这个名字更好地描述了幕后实际发生的事情。
回答by H.B.
You might as well ask why it does not implement Reverseor any other method. There simply is no reason to implement more than the most common and absolutely necessary methods. Everything beside Addand Removeis just convenience. (RemoveAllis not even part of the common interfaces that lists implement, and they do have a lot of methods already)
您不妨问为什么它没有实现Reverse或任何其他方法。除了最常见和绝对必要的方法之外,没有理由实现更多。身边的一切Add,并Remove仅仅是方便。(RemoveAll甚至不是列表实现的通用接口的一部分,它们已经有很多方法)

