zoukankan      html  css  js  c++  java
  • 创建一个对象的集合

    原文地址:http://www.codeguru.com/csharp/.net/net_general/netframeworkclasses/article.php/c13035/#more
    原名.NET Tip: Creating a Collection of Your Objects

    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:

    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.

  • 相关阅读:
    数据结构实验之栈与队列四:括号匹配(SDUT 2134)
    从 s 点到 t 点的最短路(简单模板)(迪杰斯特拉)
    畅通工程续(HDU 1874)(简单最短路)
    Til the Cows Come Home ( POJ 2387) (简单最短路 Dijkstra)
    顺序表应用7:最大子段和之分治递归法(SDUT 3664)
    Java面向对象4(P~U)
    House Lawn Kattis
    Jumbled String (Kattis
    队列详解及java实现
    栈详解及java实现
  • 原文地址:https://www.cnblogs.com/sun_moon_earth/p/598183.html
Copyright © 2011-2022 走看看