How does a list in C # work? List Base Usage Summary

Source: Internet
Author: User
What about the list in C #? The List<t> class is a generic equivalent class of the ArrayList class that implements a ilist<t> generic interface using an array of size that can be dynamically incremented on demand. Next, the small series introduces some basic simple usage of list.

The benefits of generics: It adds great potency and flexibility to writing object-oriented programs using the C # language. Performance is improved by not forcing boxing and unpacking of value types, or downward forcing type conversions on reference types.

Performance considerations: Remember that ilist<t> classes perform better and are type-safe in most cases when you decide to use ilist<t> or use the ArrayList class, which has similar functionality. If you use a reference type for type T of the Ilist<t> 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 common methods of C # list:

First, statement:

1, list<t> mlist = new list<t> ();

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

list<string> mlist = new list<string> ();

2, list<t> testlist =new list<t> (ienumerable<t> collection);

Create a list with a collection as a parameter:

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

list<string> testlist = new list<string> (Temarr);

Second, add elements:

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

Cases:

Mlist.add ("John");

2, List. AddRange (ienumerable<t> collection) add 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 at the index position

Cases:

Mlist.insert (1, "Hei");

Third, iterate through the elements in the list:

The type of foreach (Telementinmlist) T is the same as the Mlist declaration {Console.WriteLine (element);  } Example: foreach (Stringsinmlist) {Console.WriteLine (s); }


Iv. deleting elements:

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

Cases:

Mlist.remove ("Hunter");

2, List. RemoveAt (intindex); Delete the element labeled Index

Cases:

Mlist.removeat (0);

3, List. RemoveRange (Intindex,intcount);

Delete count elements starting with subscript index

Cases:

Mlist.removerange (3, 2);

V. Determine if an element is in the list:

List. Contains (T Item) returns TRUE or FALSE, which is useful

Cases:

if (Mlist.contains ("Hunter")) {Console.WriteLine ("There is Hunter in the list");  } else {Mlist.add ("Hunter");  Console.WriteLine ("Add Hunter successfully."); }


Six, to list the elements in the order:

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

Cases:

Mlist.sort ();

Seven, the list inside the element order reversal:

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

Cases:

Mlist.sort ();

Eight, List empty:

List. Clear ()

Cases:

Mlist.clear ();

Nine, 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, small c#list the basic usage of the finishing out, 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.