Eric Smith (view profile) December 18, 2006 |
One of the new language features introduced with .NET 2.0 was the generic collection. In the past, even as far back as Visual Basic 6.0, you could create a custom collection class for your classes. With .NET 1.0/1.1, you could add objects to standard collection classes like the ArrayList and the Hashtable classes. However, when you looked at the members of those collections, they were not strongly typed and had to be cast back to the original type.
The generic collection allows you to create collections of your objects without having to design a new class. Take, for example, the ValidationError class I created for a previous tip:
By using the generic List declaration, you create a collection of your custom objects without any extra work. As the snippet shows, you now can loop through the collection and .NET avoids all the extra overhead of converting a generic member of an ArrayList to a ValidationError object.
Several other generic collections are available for other situations. Refer to the help file for more information on this handy new feature.
Eric Smith is the owner of Northstar Computer Systems, a web-hosting company based in Indianapolis, Indiana. He is also a MCT and MCSD who has been developing with .NET since 2001. In addition, he has written or contributed to 12 books covering .NET, ASP, and Visual Basic. Send him your questions and feedback via e-mail at [email protected].