How to use the list in C #? List Base Usage Summary

Source: Internet
Author: User
Tags foreach sort

What about the list in C #? The list class is a generic equivalent class of the ArrayList class, which implements a IList generic interface using an array of the size that can be dynamically increased on demand. Next, the green Tea Small series to introduce some list of basic simple usage.

Benefits of generics: It adds tremendous effectiveness and flexibility to writing object-oriented programs using the C # language. Performance is improved by not forcing the boxing and unboxing of value types, or by forcing type conversions downward on reference types.

Performance considerations: When deciding whether to use IList or ArrayList classes (both have similar functionality), remember that the IList class performs better and is type-safe in most cases. If you use a reference type for the type T of the IList class, the behavior of the two classes is exactly the same. However, if you use value types for type T, you need to consider implementation and boxing issues.

Basic methods of C # list:

First, the statement:

1, List mlist = new list ();

T is the element type in the list, now as an example of string type:

List mlist = new list ();

2, List testlist =new list (IEnumerable collection);

Create a list as a parameter with a set:

String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};

List testlist = new list (Temarr);

Second, add elements:

1, List. Add (T Item) adds an element

Cases:

Mlist.add ("John");

2, List. AddRange (IEnumerable collection) adds a set of elements

Cases:

String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu"};mlist.addrange (Temarr);

3, Insert (Intindex, T item); Add an element to the index position

Cases:

Mlist.insert (1, "Hei");

Third, traversing the list elements:

The type of foreach (Telementinmlist) T is the same as the Mlist declaration

{

Console.WriteLine (Element);

}

Cases:

foreach (Stringsinmlist)

{

Console.WriteLine (s);

}

Four, delete the element:

1, List. Remove (T Item) deletes a value

Cases:

Mlist.remove ("Hunter");

2, List. RemoveAt (intindex); Delete the element with subscript index

Cases:

Mlist.removeat (0);

3, List. RemoveRange (Intindex,intcount);

Delete count elements starting at Subscript index

Cases:

Mlist.removerange (3, 2);

V. Determine whether an element is in the list:

List. Contains (T Item) returns TRUE or false, very useful

Cases:

if (Mlist.contains ("Hunter"))

{

Console.WriteLine ("There is Hunter in the list");

}

Else

{

Mlist.add ("Hunter");

Console.WriteLine ("Add Hunter successfully.");

}

The order of the elements in the list:

List. Sort () default is the first letter of the element in ascending order

Cases:

Mlist.sort ();

To reverse the order of the elements in the list:

List. Reverse () can not list. Sort () to work together to achieve the desired effect

Cases:

Mlist.sort ();

Eight, List empty:

List. Clear ()

Cases:

Mlist.clear ();

Get the number of elements in list:

List. Count () returns an int value

Cases:

In Tcount = Mlist.count ();

Console.WriteLine ("The Num of elements in the list:" +count);

The above is, green tea small weave out of the basic usage of c#list, hope to help you.

Related Article

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.