10

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?

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
Homam
  • 23,263
  • 32
  • 111
  • 187

2 Answers2

27

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.

Cody Gray - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 1
    Comment by [qtg](https://stackoverflow.com/users/7368376/qtg) incorrectly passed as [answer](https://stackoverflow.com/a/47669778/5292302) *"Can you please provide any Link regarding obsolete on Official MSDN?"* – Petter Friberg Dec 06 '17 at 08:26
  • Source for comment on obsolete (though it's a recommendation): "We don't recommend that you use the CollectionBase class for new development. Instead, we recommend that you use the generic Collection class." - https://learn.microsoft.com/en-us/dotnet/api/system.collections.collectionbase?redirectedfrom=MSDN&view=netframework-4.8#remarks – Ryan Jul 17 '19 at 20:13
4

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.

decyclone
  • 30,394
  • 6
  • 63
  • 80