Detailed usage of List in C,
C # List Detailed usage
Namespace: System. Collections. Generic
PublicclassList : IList , ICollection , IEnumerable , IList, ICollection, IEnumerable
List Class is the generic equivalent class of the ArrayList class. This class uses an array of sizes that can be dynamically increased as needed to implement IList Generic interface.
The advantage of generics: it increases the efficiency and flexibility for compiling Object-oriented Programs in c # language. It does not forcibly pack or unbox the value type, or forcibly convert the reference type downward, so the performance is improved.
Performance Considerations: when deciding to use IList Or use the ArrayList class (both have similar functions), remember IList Class performs better in most cases and is type-safe. If If the class type T uses the reference type, the behavior of the two classes is identical. However, if you use value type T, you need to consider implementation and packing.
In Microsoft's words: "Any reference or value type added to the ArrayList will be implicitly forcibly converted to an Object. If the item is of the value type, you must perform the packing operation when adding it to the list, and cancel the packing operation during retrieval. Forced conversion, packing, and unboxing all reduce performance. When circular access is required for large sets, the effects of packing and unboxing are significant ."
I. basic and common methods of List:
Statement:
1. List MList = newList ();
// T is the element type in the list. The string type is used as an example.
E. g.: List MList = newList ();
2. List TestList = new List (IEnumerable Collection );
// Create a List using a set as the parameter
E. g.: string [] temArr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu "};
List TestList = newList (TemArr );
Add element:
1. List. Add (T item)
// Add an element
E. g.: mList. Add ("John ");
2. List. AddRange (IEnumerable Collection)
// Add a group 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 to the index position
E. g.: mList. Insert (1, "Hei ");
Traverse the elements in the List:
Foreach (T element in mList)
// The type of T is the same as that of the mList statement.
{
Console. WriteLine (element );
}
E. g .:
Foreach (string s in mList)
{
Console. WriteLine (s );
}
Delete element:
1. List. Remove (T item)
// Delete a value
E. g.: mList. Remove ("Hunter ");
2. List. RemoveAt (intindex );
// Delete the element whose subscript is index
E. g.: mList. RemoveAt (0 );
3. List. RemoveRange (intindex, 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 very useful.
E. g .:
If (mList. Contains ("Hunter "))
{
Console. WriteLine ("There is Hunter in the list ");
}
Else
{
MList. Add ("Hunter ");
Console. WriteLine ("Add Hunter successfully .");
}
Sort the elements in the List:
List. Sort ()
// By default, the first letter of the element is in ascending order.
E. g.: mList. Sort ();
Reverse the order of elements in the List:
List. Reverse ()
// It Can Be Used with List. Sort () to achieve the desired effect.
E. g.: mList. Sort ();
List clearing:
List. Clear ()
E. g.: mList. Clear ();
Obtain the number of elements in the List:
List. Count ()
// Returns the int value.
E. g .:
Int count = mList. Count ();
Console. WriteLine ("The num of elements in the list:" + count );
Ii. Advanced and powerful methods of List:
Example List:
String [] temArr = {Ha "," Hunter "," Tom "," Lily "," Jay "," Jim "," Kuku "," "Locu "};
MList. AddRange (temArr );
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 Match );
Predicate is a method delegate. If the object passed to it matches the condition defined in the delegate, the method returns true. The elements of the current List are passed to the Predicate delegate one by one and moved forward in the List, starting from 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 to a Lambda expression:
E. g .:
StringlistFind = mList. Find (name => {
If (name. Length> 3 ){
Returntrue;
}
Returnfalse;
}
);
// Name is a variable, representing the mList
// Set the element in.
Console. WriteLine (listFind );
// The output is Hunter.
Delegate to a function:
E. g .:
String listFind1 = mList. Find (ListFind); // Delegate to the ListFind Function
Console. WriteLine (listFind); // The output is Hunter.
ListFind function:
Public bool ListFind (string name)
{
If (name. Length> 3)
{
Returntrue;
}
Returnfalse;
}
The results of the two methods are the same.
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.
Public T FindLast (Predicate Match );
The usage is the same as that of List. Find.
List. TrueForAll method: determines whether each element in the List matches the condition defined by the specified predicate.
Publicbool TrueForAll (Predicate Match );
Delegate to lambda expressions:
E. g .:
Bool flag = mList. TrueForAll (name =>
{
If (name. Length> 3)
{
Returntrue;
}
Else
{
Returnfalse;
}
}
);
Console. WriteLine ("True for all:" + flag); // The flag value is false.
Delegate to a function. The above ListFind function is used here:
E. g.: bool flag = mList. TrueForAll (ListFind); // Delegate to the ListFind Function
Console. WriteLine ("True forall:" + flag); // The flag value is false.
The results of the two methods are the same.
List. FindAll method: Retrieves all elements that match the conditions defined by the specified predicate.
PublicList FindAll (Predicate Match );
E. g .:
List SubList = mList. FindAll (ListFind); // Delegate to the ListFind Function
Foreach (string s in subList)
{
Console. WriteLine ("element in subList:" + s );
}
SubList stores all elements with a length greater than 3.
List. Take (n): returns IEnumetable from the first n rows. , T type and List Same type
E. g .:
IEnumerable TakeList = mList. Take (5 );
Foreach (string s intakeList)
{
Console. WriteLine ("element in takeList:" + s );
}
In this case, takeList stores the first five elements in mList.
List. Where: Retrieves all elements that match the conditions defined by the specified predicate. Similar to the List. FindAll method.
E. g .:
IEnumerable WhereList = mList. Where (name =>
{
If (name. Length> 3)
{
Returntrue;
}
Else
{
Returnfalse;
}
});
Foreach (string s in subList)
{
Console. WriteLine ("element in subList:" + s );
}
SubList stores all elements with a length greater than 3.
List. RemoveAll: removes all elements that match the conditions defined by the specified predicate.
Publicint RemoveAll (Predicate Match );
E. g .:
MList. RemoveAll (name =>
{
If (name. Length> 3)
{
Returntrue;
}
Else
{
Returnfalse;
}
});
Foreach (string s in mList)
{
Console. WriteLine ("element in mList:" + s );
}
In this case, the mList stores the elements with a length greater than 3.