Using the list class in C #

Source: Internet
Author: User

The use of the List<t> class in the System.Collection.Generic namespace is very similar to the Arraylisy class of the System.Collection namespace. This class implements the Ilist,icollection and IEnumerable interfaces,

Example: The Racer class acts as an element added to the collection, indicating the racer, including two fields: Name and car, available properties to access

  public class racer
        {
             private string name;
            public string Name
             {
                 Get
                 {
                     return name;
               }
           }

            private string car;
            public string car
             {
                 Get
                 {
                     return car;
               }
           }

Public Racer (string name, string car)
{
THIS.name = name;
This.car = car;
}

public override string ToString ()
{
Return name + ', ' + car;
}
}

The racer variable is defined as the list<racer> type. Creates a new object of the type using the new operator. This uses the default constructor of the List<t> class. Another constructor of this class reserves memory space for a specific number of elements, and a constructor is responsible for copying elements from a collection of list<t> types.

Because the List<t> class is instantiated with a specific racer class, you can now only add racer objects with the Add () method, in the List<t> class, the Add () method is defined as void Add (T item). In the following example code, create an instance, And added to the collection. Then use the foreach statement to iterate through each player in the collection and display to the console:

List<racer> racers = new list<racer> ();
Racers. ADD (new Racer ("MS", "F"));
Racers. ADD (new Racer ("JPM", "MM"));
Racers. ADD (new Racer ("KR", "MM"));
Racers. ADD (new Racer ("MW", "WB"));
Racers. ADD (new Racer ("RB", "F"));

foreach (Racer R in Racers)
{
Console.WriteLine (R);
}

In the List<t> class, you can use enumerations not only to add and access elements, but to insert and delete elements, empty collections, and copy elements into an array. More powerful features are discussed below. List<t> provides methods to search and convert elements, to reverse elements, and so on.

1 Finding elements

The List<t> class provides the find () and FindAll () methods, and their declarations are as follows:

Public T-FIND (predicate <T> match)

Public T findall (predicate <T> match)

Both of these methods take predicate <T> as parameters. predicate <T> is a delegate that references a predicate method. A predicate is a method that returns a Boolean value. True indicates that there is a match, false means no. When True is find returns the first match, FindAll returns all elements. You define predicates before you use them.


         public class Findracer
         {
            private string car;
            public findracer (string car)
             {
                 This.car = car;
           }
            public bool Drivingcarpredicate (racer racer )
            {
                 return racer. Car = = Car;
           }
       }

To find a specific player, you should start and initialize the Findracer class with F. Use the FindAll () method of the List<t> class. FindAll method, instantiating a predicate delegate, which is commissioned to accept the finder. Drivingcarpredicate method. FindAll () returns a list of list<racer> types, and then uses foreach to iterate through all the returned Cylons and display them on the console:

List<racer> racers = new list<racer> ();
Racers. ADD (new Racer ("MS", "F"));
Racers. ADD (new Racer ("JPM", "MM"));
Racers. ADD (new Racer ("KR", "MM"));
Racers. ADD (new Racer ("MW", "WB"));
Racers. ADD (new Racer ("RB", "F"));

Findracer finder = new Findracer ("F");
foreach (Racer racer in racers. FindAll (Finder, new predicate<racer>. drivingcarpredicate)))
{
Console.WriteLine (racer);

}

This article from Csdn Blog, reproduced please indicate the source: http://blog.csdn.net/hwg9741/archive/2008/06/03/2506345.aspx

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.