CollectionBase versus ArrayList

J

Jason

All the examples I have seen about typed collections says to inherit from
CollectionBase and implement type-specific Add and Remove methods.
Considering that CollectionBase uses an ArayList for the actual list storage
(i.e. InnerList property), why would you do this instead of just inheriting
from ArrayList and overridding Add and Remove?

- Jason
 
J

Jon Skeet [C# MVP]

Jason said:
All the examples I have seen about typed collections says to inherit from
CollectionBase and implement type-specific Add and Remove methods.
Considering that CollectionBase uses an ArayList for the actual list storage
(i.e. InnerList property), why would you do this instead of just inheriting
from ArrayList and overridding Add and Remove?

Because if you inherit from ArrayList, people can easily inadvertently
call the wrong overload. Inheriting from CollectionBase stops people
from using the wrong overload unless they specifically treat it as an
IList.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top