Generic Collections in .NET 2.0

posted on 06/04/05 at 12:07:26 am by Joel Ross

I'm starting to dig into Generics and collections for the Tourney Bracket Control. We have a number of collections that we use, and before, we had a lot of (generated) code wrapped into those collections. With generics, we can eliminate most of that code.

Each of our collections has an "Owner" property, which allows us to walk the bracket hierarchy - go from a round to it's containing group, or from a match up to it's containing round. Based on these requirements, I need to have a custom collection and can't use a generic list, but I still want the maintainability of generics. Well, it turns out you can inherit from a generic collection and make a typed collection:

MyTypedCollection : System.Collections.Generic.List<MyTypedObject>

You can inherit from a generic collection, specifying a particular type when you do so. Now I can add my methods and properties that I need for each collection. I cut my collections down from a few 100 lines of code to less than 20. Nice!

Categories: Blogging