1 About the introduction
(1) Owning namespace: System.Collections.Generic
public class list<t>: Ilist<t>, Icollection<t>, Ienumerable<t>, IList, ICollection, IEnumerable.
(2) 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.
(3) Benefits of generics: It adds great potency and flexibility to writing object-oriented programs in the C # language. Performance is improved by not forcing boxing and unpacking of value types, or downward forcing type conversions on reference types.
(4) Performance considerations: When deciding whether to use List<t> or the ArrayList class, which has similar functionality, remember that the List<t> class performs better and is type-safe in most cases.
If you use a reference type for type T of the List<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.
(5) Speaking in Microsoft's words:
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. ”
Common methods
1 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
such as: 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
String[] Temarr = {"Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "LOCU"};
list<string> testlist = new list<string> (Temarr);
2 adding elements:
(1) List. Add (T Item) adds an element
Mlist.add ("John");
(2) List. AddRange (ienumerable<t> collection) add a set of elements
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
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);
Following
foreach (string s in mlist)
{
Console.WriteLine (s);
}
2 Deleting an element
(1) List. Remove (T Item) deletes a value
such as: Mlist.remove ("Hunter");
(2) List. RemoveAt (int index); Delete the element labeled Index
such as mlist.removeat (0);
(3) List. RemoveRange (int index, int count);
Delete count elements starting with subscript index
such as Mlist.removerange (3, 2);
3 Determine whether an element is in the list:
List. Contains (T Item) returns TRUE or FALSE, which is useful
if (Mlist.contains ("Hunter"))
{
Console.WriteLine ("There is Hunter in the list");
}
Else
{
Mlist.add ("Hunter");
Console.WriteLine ("Add Hunter successfully.");
}