CollectionBase 类是否仍然受支持?
我想创建一个继承自 CollectionBase
,但是好像不支持LINQ扩展!
还支持吗?或者有其他解决方案吗?
I want to create a class that inherits from CollectionBase
, but it seems that it does not support LINQ extensions!
Is it still supported? Or is there an alternative solution?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
它仍然受支持,但已过时。您应该始终更喜欢使用
System.Collections.Generic< 中定义的集合/code>
或
System.Collections.ObjectModel
命名空间。您也可以从通用集合之一继承来创建您自己的自定义强类型集合。而且它将完全支持 LINQ,因为它们已经实现了
IEnumerable
。List
或Collection
是适合您情况的不错选择。如果可能的话,请避免使用非泛型集合,例如
CollectionBase
、ArrayList
和HashTable
。使用它们会带来性能损失,并且与框架的最新版本中引入的通用版本相比,它们几乎没有什么优势(如果有的话!)。It is still supported, but it is obsolete. You should always prefer to use the collections defined in the
System.Collections.Generic
or theSystem.Collections.ObjectModel
namespaces, instead.You can inherit from one of the generic collections to create your own custom, strongly-typed collection, as well. And it will have full support for LINQ, since they already implement
IEnumerable<T>
.Either
List<T>
orCollection<T>
are good options for your case.Avoid the non-generic collections like
CollectionBase
,ArrayList
, andHashTable
if at all possible. There is a performance penalty to using them, and they offer few advantages (if any!) over the generic versions that were introduced in more recent versions of the framework.Linq 扩展是为
IEnumerable编写的
(通用)和CollectionBase(非通用)不会继承它。这就是为什么您无法在其上使用 Linq 的原因。使用
System.Collections.ObjectModel.Collection
< /a> 相反。Linq extensions are written for
IEnumerable<T>
(Generic) and CollectionBase (Non-generic) does not inherit from it. That is why you are not able to use Linq on it.Use
System.Collections.ObjectModel.Collection<T>
instead.