Defines a strongly typed list of objects that the:list<t> class represents that can be accessed through an index, and provides methods for searching, sorting, and manipulating the list.
Role:
The most common use of generics is generic collections
When we create a list class, the data type of the list item may be int,string or other type, and if you treat the list class the same way,
It is not necessary to specify the data type beforehand, and then specify it when the list class is instantiated. Equivalent to the data type as a parameter, which can be most
Reuse code, protect types of security, and improve performance.
General usage of List
Owning namespace: System.Collections.Generic
public class List<t>:ilist<t>,icollection<t>,ienumerable<t>,ilist,icollection, Ienumerable
List<t> 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
(1) Declaration list<t>mlist = new List<t> ();
Eg:string[] Arr = {"A", "B", "C"};
list<string> mlist = new list<string> (ARR);
(2) Add an element List.add (T Item)
Eg:mlist. ADD ("D");
(3) Adding a collection element
Eg:string[] ARR2 ={"F", "G". " H "};
Mlist. AddRange (ARR2);
(4) Add an element to the index position Insert (int index,t Item)
Eg:mlist. Insert (1, "P");
(5) iterating through the elements in the list
The type of the foreach (t element in Mlist) T is the same as the Mlist declaration
{
Console.WriteLine (Element);
}
eg
foreach (string s in mlist)
{
Console.WriteLine (s);
}
(6) Deleting an element
List.remove (T Item) Delete a value
Eg:mlist. Remove ("a");
List.removeat (int index); Delete the element labeled Index
Eg:mlist. RemoveAt (0);
List.removerange (int index,int count); Subscript index Start, delete count elements
Eg:mlist. RemoveRange (3,2);
(7) Determine if an element is in the list
List.contains (T Item) returns TRUE or False
eg
if (mlist. Contains "(" G "))
Console.WriteLine ("G exists in List");
Else
Mlist. ADD ("G");
(8) Sort the elements in the List List.sort () by default, each letter of the element is ascending
Eg:mlist. Sort ();
(9) to the List inside the element order reversal List.reverse () can be used in conjunction with List.sort ()
(10) List Empty list.clear ()
Eg:mlist. Clear ();
(11) Get the number of elements in list List.count () returns an int value
Eg:mlist.count ();
List advanced, powerful method
(1) List.findall method: Retrieves all elements that match the conditions defined by the specified predicate
Class Program
{
static void Main (stirng[] args)
{
Student stu = new Student ();
Stu. Name= "Arron";
list<student> students= new list<student> ();
Students. ADD (Stu);
Students. ADD (New Student ("Candy"));
FindName myname = new FindName ("Arron");
foreach (student s in students. FindAll (New predicate<student> (myname. Isname)))
{Console.WriteLine (s);}
}
public class student
{
public string Name{get;set;}
Public student () {}
public override string ToString ()
{
return string. Format ("name: {0}", name);
}
}
public class FindName
{
private string _name;
Public FindName (String Name)
{this._name=name;}
public bool Isname (student s)
{return (s.name==_name)? True:false;}
}
(2) The 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
eg
predicate is a delegate to a method that returns true if the object passed to it matches the condition defined by the delegate, and the element of the current list
is passed to the predicate delegate one by one and moves forward in the middle of the list, starting with the first element, ending with the last element, when a match is found
When processing stops
The first method delegates to the lambda expression:
eg
String listfind = Mlist. Find (name=>
{
if (name.length>3)
return true;
return false;
});
The second method delegates to a function
eg
public bool Listfind (string name)
{
if (name. Length > 3)
{
return true;
}
return false;
}
The results of these two methods are the same
(3) List.findlast method public T FindLast (predicate<t> match); Determines whether each element in the list matches the conditions defined by the specified predicate. Usage is the same as List.find.
(4) List.trueforall method: Determines whether each element in the list matches the conditions defined by the specified predicate.
public bool Trueforall (predicate<t> match);
(5) List.take (n): Gets the first n rows the return value of 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.
(6) 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
(7) 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.