.NET Tip: Creating a Collection of Your Objects

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:

public class ValidationError
{
   private string _error;

   public string ErrorMessage
   {
      get { return _error; }
      set { _error = value; }
   }

   public ValidationError(string errorMessage)
   {
      ErrorMessage = errorMessage;
   }
}

If you wanted to create a collection of ValidationError objects, you could put them in an ArrayList. However, using a generic collection as shown in the following snippet requires a bit less overhead:

   List<ValidationError> errors = new List<ValidationError>();

   errors.Add(new ValidationError("Error #1"));
   errors.Add(new ValidationError("Error #2"));
   errors.Add(new ValidationError("Error #3"));
   errors.Add(new ValidationError("Error #4"));

   foreach (ValidationError err in errors)
   {
      Response.Write(err.ErrorMessage + "<br>");
   }

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.

About the Author

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 questions@techniquescentral.com.



Comments

  • how is this useful ?

    Posted by leonbay on 10/03/2007 11:16pm

    can you show a situation how this can be useful?

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 17, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Another day, another end-of-support deadline. You've heard enough about the hazards of not migrating to Windows Server 2008 or 2012. What you may not know is that there's plenty in it for you and your business, like increased automation and performance, time-saving technical features, and a lower total cost of ownership. Check out this upcoming eSeminar and join Rich Holmes, Pomeroy's practice director of virtualization, as he discusses the …

  • Live Event Date: September 16, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you starting an on-premise-to-cloud data migration project? Have you thought about how much space you might need for your online platform or how to handle data that might be related to users who no longer exist? If these questions or any other concerns have been plaguing you about your migration project, check out this eSeminar. Join our speakers Betsy Bilhorn, VP, Product Management at Scribe, Mike Virnig, PowerSucess Manager and Michele …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds