Use of list in C #

Source: Internet
Author: User

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);

1. Adding elements

(1) List. Add (T Item) adds an element, e.g.: Mlist.add ("John");

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

e.g.: string[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};

Mlist.addrange (Temarr);

(3) Add an element at index position , e.g.: Mlist.insert (1, "Hei");

2. deleting an element

(1) Delete a value, e.g.: Mlist.remove ("Hunter");

(2) Delete the element labeled index , e.g.: Mlist.removeat (0);

(3) Starting from Subscript Index , delete count elements, e.g.: Mlist.removerange (3, 2);

3. Sorting elements

(1) The default is the first letter of the element in ascending order, e.g.: Mlist.sort ();

(2) sequential reversal, e.g.: List. Reverse (); can be associated with list. Sort () to use to achieve the desired effect

4. Find elements

(1) 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.

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.

Public T Find (Predicate<t>match);

Delegate to an lambda expression:

e.g.:

1  stringlistfind = mlist.find (name = =  )//name is a variable, which represents mlist 2  3             {                              //element, setting itself 4  5                 if ( Name. Length > 3)             6               {7                     returntrue; 8                 } 9                 returnfalse;11             };13            Console.WriteLine (listfind);     Output is Hunter

Delegate to a function :

1 String listFind1 = Mlist.find (listfind);   Delegate to the Listfind function 2 Console.WriteLine (listfind);           The output is Hunter 3  4//listfind function: 5 public bool Listfind (string name) 6         {7             if (name). Length > 3) 8             {9                 returntrue;10             }11             returnfalse;12          }

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.

(2) List.trueforall Method: Determines whether each element in the list matches the conditions defined by the specified predicate.

Publicbool Trueforall (predicate<t> match);

Delegate to an lambda expression:

1 bool Flag=mlist.trueforall (name=>) 2 {3 if (name). LENGTH>3) 4 {5 return true; 6} 7 Else 8 {9 return false;10}11}12 Console.WriteLine ("True for All:" +flag);//flag value to False

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

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

(3) List.findall Method: Retrieves all elements that match the conditions defined by the specified predicate.

Publiclist<t>findall (predicate<t> match);

1 e.g.: 2  3 list<string> sublist =mlist.findall (listfind);//Delegate to Listfind function 4  5         foreach (String s in Sub List) 6  7         {8  9             Console.WriteLine ("Element in Sublist:" +s);         }12         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 List<t> class//Type 16 17 E.g.:18 ienumerable<string>takelist=  Mlist.take (5);           foreach (string s intakelist)               Console.WriteLine ("Element in Takelist:" + s);           }28      //  at this point the takelist element is the first 5 in Mlist

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

(4) List.removeall Method: Removes all elements that match the conditions defined by the specified predicate.

Use of list in C #

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.