Peter Duniho wrote:
[...]
The reason for not doing it via inheritance is, as I pointed out
earlier, actually overloading a method in an existing collection class
should generally be avoided, mainly because inheriting an existing
collection class should be avoided.
Do you mind to give some particular cases on why inheriting a collection
class should be avoided?
Inheriting _any_ class should be avoided. The word "avoid" doesn't mean
you should never do it. It just means that you should prefer to solve
your problem in another way.
Inheritance creates a very close relationship between your new class and
the base class. It also _creates a whole new class_! Generics are
complicated enough, where you get a whole new class just by using a
different type parameter. So to create yet another whole new class just
to add a single method just compounds that issue.
It's my opinion that these general rules of thumb apply with extra
strength to the built-in classes, at least those not specifically designed
for inheritance in mind (e.g. abstract classes like Stream,
CollectionBase, etc.), because the benefits tend not outweigh the added
complexity. It's not a hard-and-fast rule -- one thing inheritance allows
you to do is store additional data with the instance, and that's a lot
more complicated to do without inheritance -- but it should be the guiding
principle, going against it only when one has considered carefully the
reasons and alternatives and has decided that inheritance is clearly the
best solution.
Another issue is that sometimes people try to inherit a collection class
to hide members of the base class. That doesn't seem to be the case here,
but it's another example of where inheriting the class is a bad idea.
Member hiding is almost never the right approach, since in spite of the
use of the word "hide", it's not a security feature at all (i.e. you can
hide a member, but the hidden member is always still accessible to code
that wants it or, worse, to code that doesn't realize there's a derived
class hiding the member).
Basically, the general OOP rule is that if there's a simple way to avoid
inheriting a class, then do that instead of inheriting a class.
Inheritance should be used where it makes the code _better_ in some
observable way. That is not the case here.
I had no problem in the past but well I'm willing to learn. I think by
limiting oneself like this will only limiting the full potential of the
.Net framework itself. If it is really that bad then I think the
framework author would have sealed the class long before...
Just because a class isn't sealed, that doesn't mean that inheriting the
class is always the right way to a solution.
Pete