I'm not looking for how, I'm looking for why? I couldn't find a straight forward answer to this.
A generic collection is strongly typed (you can store one type of objects into it) so that we can eliminate runtime type mismatches, it improves the performance by avoiding boxing and unboxing.
Contains interfaces and classes that define generic collections, which allow users to create strongly typed collections that provide better type safety and performance than non-generic strongly typed collections.
The key difference between Generic and Non-generic Collection in C# is that a Generic Collection is strongly typed while a Non-Generic Collection is not strongly typed.
Better Performance with Generics As you have seen in the above example, at the time of fetching the data from the ArrayList collection we need to do type casting which causes performance degrades. But at the time of fetching the data from the generic List, we don't require to do type casting.
Yes. CollectionBase was a previous attempt, and a way to provide type safety.
Generics give you these advantages, but add two more HUGE advantages:
Edit:
I thought of a couple of other compelling reasons to move your code to using generic collections:
Cast<T>
(CollectionBase does not implement IEnumerable<T>
, only IEnumerable
).Strongly typed access to all the different kinds of collections at the cost of only typing <type>
?
EDIT: If you are removing existing and working code, the only reason would be for performance as mentioned elsewhere.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With