Why should I replace CollectionBase with Generics?

.net, c#, collections, generics

Solution

Yes. CollectionBase was a previous attempt, and a way to provide type safety.

Generics give you these advantages, but add two more HUGE advantages:

- With generics, you no longer have boxing and unboxing at every access to your collection. This provides a huge perf. advantage.

- With generics, you can use a single implementation for all of your types. With CollectionBase, each type required a custom implementation, which leads to a huge amount of duplicated code (ie: potential for bugs).

Edit:

I thought of a couple of other compelling reasons to move your code to using generic collections:

- Using generic collections will allow you to directly use LINQ to Objects on your collections, without requiring calls to `Cast<T>` (CollectionBase does not implement `IEnumerable<T>`, only `IEnumerable`).

- Provide consistency with any new code, which should always be done using the new generic collections.

Problem

I'm not looking for how, I'm looking for why? I couldn't find a straight forward answer to this.

Original source