1. Basics and common methods of List:
(1). Statement:
①. List<T> mList = new List<T>();
T is the type of elements in the list. Now take the string type as an example.
List<string> mList = new List<string>();
②、List<T> testList =new List<T> (IEnumerable<T> collection);
Create a List with a collection as argument:
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" }; List<string> testList = new List<string>(temArr);
(2), add elements:
①、Add an element
Syntax: List. Add(T item)
List<string> mList = new List<string>(); mList.Add("John");
②. Add a set of elements
语法: List. AddRange(IEnumerable<T> collection)
List<string> mList = new List<string>(); string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" }; mList.AddRange(temArr);
③. Add an element at the index position
Syntax: Insert(int index, T item);
List<string> mList = new List<string>(); mList.Insert(1, "Hei");
④. Traverse the elements in the List
grammar:
foreach ( T element in mList) //The type of T is the same as when mList was declared { Console.WriteLine(element); }
example:
List<string> mList = new List<string>(); ...//Omit some code foreach (string s in mList) { Console.WriteLine(s); }
(3) Delete elements:
①.Delete a value
Syntax: List.Remove(T item)
mList.Remove("Hunter");
②. Delete the element whose subscript is index.
Syntax: List.RemoveAt(int index);
mList.RemoveAt(0);
③. Starting from the subscript index, delete count elements.
Syntax: List.RemoveRange(int index, int count);
mList.RemoveRange(3, 2);
(4) Determine whether an element is in the List:
Syntax: List. Contains(T item) Return value: true/false
if (mList.Contains("Hunter")) { Console.WriteLine("There is Hunter in the list"); } else { mList.Add("Hunter"); Console.WriteLine("Add Hunter successfully."); }
(5) Sort the elements in the List:
Syntax: List. Sort () The default is the first letter of the element in ascending order
mList.Sort();
(6) Reverse the order of the elements in the List:
Syntax: List.Reverse () can be used in conjunction with List.Sort () to achieve the desired effect
mList. Reverse();
(7). Clear the List:
Syntax: List.Clear()
mList.Clear();
(8). Obtain the number of elements in the List:
Syntax: List.Count () returns int value
int count = mList.Count(); Console.WriteLine("The num of elements in the list: " +count);
2. Advanced and powerful methods of List:
List used in this paragraph as an example:
string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", " "Locu" }; mList.AddRange(temArr);
(1), List.FindAll method : Retrieve all elements that match the conditions defined by the specified predicate
语法:public List<T> FindAll(Predicate<T> match);
List<string> subList = mList.FindAll(ListFind); //Delegate to ListFind function foreach (string s in subList) { Console.WriteLine("element in subList: "+s); }
At this time, subList stores all elements with a length greater than 3.
(2) List.Find method : Search for elements that match the conditions defined by the specified predicate, and return 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 conditions defined in the delegate. The elements of the current List are passed to the Predicate delegate one by one and moved 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 a lambda expression:
Delegate to lambda expression:
string listFind = mList.Find(name => //name is a variable, representing the elements in mList, set it yourself { if (name.Length > 3) { return true; } return false; }); Console.WriteLine(listFind); //The output is Hunter
Delegate to a function:
string listFind1 = mList.Find(ListFind); //Delegate to ListFind function Console.WriteLine(listFind); //The output is Hunter //ListFind function public bool ListFind(string name) { if (name.Length > 3) { return true; } return false; }
The results of both methods are the same.
(3) List.FindLast method : Search for elements that match the conditions defined by the specified predicate, and return the last matching element in the entire List.
Syntax: public T FindLast(Predicate<T> match);
Usage is the same as List.Find.
(4), List.TrueForAll method : Determine whether each element in the List matches the conditions defined by the specified predicate.
Syntax: public bool TrueForAll(Predicate<T> match);
Delegate to lambda expression:
bool flag = mList.TrueForAll(name => { if (name.Length > 3) { return true; } else { return false; } }); Console.WriteLine("True for all: "+flag); //flag value is
Delegate to a function, the above ListFind function is used here:
bool flag = mList.TrueForAll(ListFind); //Delegate to ListFind function Console.WriteLine("True for all: "+flag); //flag value is false
The results of both methods are the same.
(5) List.Take(n) method: Get the first n rows and the return value is IEnumetable<T>, the type of T is the same as the type of List<T>
IEnumerable<string> takeList= mList.Take(5); foreach (string s in takeList) { Console.WriteLine("element in takeList: " + s); }
At this time, the elements stored in takeList are the first 5 elements in mList.
(6) List.Where method : Retrieve all elements that match the conditions defined by the specified predicate. Similar to the List.FindAll method.
IEnumerable<string> whereList = mList.Where(name => { if (name.Length > 3) { return true; } else { return false; } }); foreach (string s in subList) { Console.WriteLine("element in subLis
At this time, subList stores all elements with a length greater than 3.
(7) List.RemoveAll method: Remove all elements that match the conditions defined by the specified predicate.
语法: public int RemoveAll(Predicate<T> match);
mList.RemoveAll(name => { if (name.Length > 3) { return true; } else { return false; } }); foreach (string s in mList) { Console.WriteLine("element in mList: " + s); }