C # Generic Collection list<t> Usage Summary

Source: Internet
Author: User

List<t> is a fast, easy-to-use generic collection type in C # applications that uses generic programming to increase the efficiency and flexibility of writing object-oriented programs, without forcing boxing and unboxing of value types, or downward forcing type conversions on reference types.

Additional notes:

When deciding whether to use Ilist<t> or the ArrayList class, which has similar functionality, remember that the Ilist<t> class performs better and is type-safe in most cases.

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.

Any reference or value type added to ArrayList is implicitly cast upward to Object. If the item is a value type, it must be boxed when it is added to the list, and the unboxing operation is performed when it is retrieved. Both casting and boxing and unboxing operations degrade performance, and the effect of boxing and unboxing is noticeable in cases where large collections must be cycled. ”

First, list<t>basic usage of:1. Create list<t>class specific instances:1.1, create an ordinary generic collection class:1List<T> mlist =NewList<t>(); T is the element type in the list, and now takes the string type as an example1List<string> mlist =Newlist<string>();1.2, creating a new generic collection as a set of parameters list<t>1List<T> testlist =NewList<t> (ienumerable<t>collection); Here is a specific example:12string[] Temarr = {"Ha","Hunter","Tom","Lily","Jay","Jim","Kuku","LOCU" }; List<string> testlist =Newlist<string>(Temarr);2. Add elements to List<t>:2.1, add an element:1List. ADD (T Item) Example:1Testlist.add ("John");2.2, add a set of element collections:1List. AddRange (IEnumerable<T>collection) Example:12string[] Temarr = {"Ha","Hunter","Tom","Lily","Jay","Jim","Kuku","LOCU"};testlist.addrange (Temarr);2.3, add an element at the specified location:1Insert (intindex, T item); Where index specifies the location where the element is to be added, example:1Testlist.insert (1,"Hei");3. Generic collection list<t>element traversal: Use foreach to easily traverse the list<T>examples of all elements in:123foreach(stringSinchmlist) {Console.WriteLine (s);}
4. Delete list<t>the elements in:4.1, delete a single element: List. The Remove (T Item) item specifies the object to delete the element from, example: Mlist.remove ("Hunter");4.2, delete the element at the specified location: list.removeat (intIndex ), which, index specifies the indexed value for the element to be deleted, example: Mlist.removeat (0);4.3, delete multiple elements: List.removerange (intIndexintcount), index specifies the starting position of the deleted element, and count specifies the number of elements to be deleted starting from the starting position, example: Mlist.removerange (3,2);5. Determines whether an element is in the list<t>Medium: List.contains (T Item) This method is used to determine whether an element is in the List< (of < (t>) >), if Item is found in list< (of < (t>) >);true, otherwise thefalse, Example:if(Mlist.contains ("Hunter") {Console.WriteLine ("there is Hunter in the list");}Else{Mlist.add ("Hunter"); Console.WriteLine ("Add Hunter successfully.");}6. To List<t>inside Elements Sort: list.sort () The method sets the collection class List<T>the elements in the default way elements are sorted in ascending order of the first letter. Mlist.sort ();7. To List<t>inside the element order reversal: List.reverse () The method can be associated with List. Sort () to use to achieve the desired effect. Mlist.sort ();8. Empty list<t>all elements in: List.clear () This method clears the List<T>all elements in, example: Mlist.clear ();9. Get list<t>number of elements in: list. Count () The method returns a list<T>int type value of the number of elements in, example:intCount =Mlist.count (); Console.WriteLine ("The num of elements in the list:"+count);

C # Generic Collection list<t> Usage Summary

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.