Summary of usage of C # list<t>

Source: Internet
Author: User
Owning namespace: System.Collections.Generic
public class list<t>: Ilist<t>, Icollection<t>, Ienumerable<t>, IList, ICollection, IEnumerable

The List<t> class is a generic equivalent class of the ArrayList class. This class implements the Ilist<t> generic interface using an array of size that can be dynamically incremented on demand.

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:

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. ”


1, the basis of list, common methods:

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

e.g.: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

e.g.:
String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};
list<string> testlist = new list<string> (Temarr);


To add an element:

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

E.g.:mlist.add ("John");

2, List. AddRange (ienumerable<t> collection) add a set of elements

e.g.:
String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};
Mlist.addrange (Temarr);

3, Insert (int index, T item); Add an element at the index position

E.g.:mlist.insert (1, "Hei");

To traverse the elements in a list:

The type of the foreach (t element in mlist)  T is the same as the Mlist declaration {    Console.WriteLine (element);}

e.g.:

foreach (string s in mlist) {    Console.WriteLine (s);}

To delete an element:


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

E.g.:mlist.remove ("Hunter");

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

E.g.:mlist.removeat (0);

3, List. RemoveRange (int index, int count);

Delete count elements starting with subscript index

E.g.:mlist.removerange (3, 2);

Determine whether an element is in the list:

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

e.g.:

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

To sort the elements in the list:


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

E.g.:mlist.sort ();

Reverses the order of the elements inside the list:

List. Reverse () can be associated with list. Sort () to use to achieve the desired effect

E.g.:mlist.sort ();

List empty: List. Clear ()
e.g.: Mlist.clear ();

Get the number of elements in list:

List. Count () returns an int value

e.g.:
int count = Mlist.count ();
Console.WriteLine ("The Num of elements in the list:" +count);

2, the list of advanced, powerful method:

For example, list:

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

Mlist.addrange (Temarr);

List.find method: Searches for an element that matches the conditions defined by the specified predicate and returns the first matching element in the entire list.
Public T Find (predicate<t> match);

predicate is a delegate to a method that returns true if the object passed to it matches the condition defined in the delegate. The elements of the current list are passed to the predicate delegate one by one and move forward in the list, starting with the first element and ending with the last element. Processing stops when a match is found.

predicate can be delegated to a function or to a lambda expression:

Delegate to an lambda expression:

e.g.:

String listfind = Mlist.find (name =  //name is a variable that represents the Mlist   {      ///element, set itself   if (name). Length > 3)   {  return true;   }  return false;}); Console.WriteLine (listfind);     Output is Hunter

Delegate to a function:


e.g.:
String listFind1 = Mlist.find (Listfind); Delegate to the Listfind function
Console.WriteLine (Listfind); Output is Hunter

Listfind function:

public bool Listfind (string name) {if (name. Length > 3) {    return true;} return false; }


The results of these two methods are the same.

List.findlast method: Searches for an element that matches the conditions defined by the specified predicate and returns the last matching element in the entire list.
Public T FindLast (predicate<t> match);


Usage is the same as List.find.

List.trueforall method: Determines whether each element in the list matches the conditions defined by the specified predicate.

public bool Trueforall (predicate<t> match);

Delegate to an lambda expression:

e.g.:

BOOL flag = Mlist.trueforall (name =>{    if (name). Length > 3)    {return true;    }    Else    {return false;    }}); Console.WriteLine ("True for All:  " +flag);  Flag value is False

Delegate to a function, where the Listfind function is used:

e.g.:

BOOL flag = Mlist.trueforall (Listfind); Delegate to the Listfind function
Console.WriteLine ("True for All:" +flag); Flag value is False

The results of these two methods are the same.

List.findall method: Retrieves all elements that match the conditions defined by the specified predicate.

Public list<t> FindAll (predicate<t> match);

e.g.:

list<string> sublist = Mlist.findall (Listfind); Delegate to the Listfind function foreach (string s in sublist) {Console.WriteLine ("element in Sublist:" +s);}

At this point sublist stores all elements with a length greater than 3

List.take (N): The type of the first n row return value ienumetable<t>,t is the same as the type of list<t>


e.g.:

Ienumerable<string> takelist=  Mlist.take (5);   foreach (string s in Takelist)   {  Console.WriteLine ("Element in Takelist:" + s);   }

The takelist element is the first 5 in the mlist.

List.where method: Retrieves all elements that match the conditions defined by the specified predicate. Similar to the List.findall method.

e.g.:

ienumerable<string> wherelist = mlist.where (name = =    {if (name). Length > 3) {return true;} else {return false;}    });  foreach (string s in sublist)  {Console.WriteLine ("element in Sublist:" +s);  

At this point sublist stores all elements with a length greater than 3

List.removeall method: Removes all elements that match the conditions defined by the specified predicate.


public int RemoveAll (predicate<t> match);

e.g.:

Mlist.removeall (name = =    {if (name). Length > 3) {return true;} else {return false;}    }); foreach (string s in mlist) {    Console.WriteLine ("Element in Mlist:     " + s);}

At this point, mlist stores the element that is longer than 3.

List<t> is a generic linked list ... T represents the node element type
Like what
List<int> intlist; a linked list of elements that are int
Intlist.add (34); Add to
Intlist.remove (34);//delete
Intlist.removeat (0); Delete an element that is located somewhere
Intlist.count; Linked list length
There are insert,find,findall,contains and other methods, there are indexing methods intlist[0] = 23;
1. Reduced packing and unpacking
2. Easy to check data type at compile time

List<object> is equivalent to the list inside the System.Collections namespace.

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.