Is the CollectionBase class still supported?

前端 未结 2 585
清酒与你
清酒与你 2020-12-09 20:51

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

相关标签:
2条回答
  • 2020-12-09 21:19

    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.

    0 讨论(0)
  • 2020-12-09 21:24

    It is still supported, but it is obsolete. You should always prefer to use the collections defined in the System.Collections.Generic or the System.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> or Collection<T> are good options for your case.

    Avoid the non-generic collections like CollectionBase, ArrayList, and HashTable 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.

    0 讨论(0)
提交回复
热议问题