List generic usage of the Internet, not verified, visual basic right, teaching materials.
1. List of basic, common methods:
statement:
1、List<T> mList = new List<T>();
T is the list element type, string type as an example now to
E.g.: List<string> mList = new List<string>();
2、List<T> testList =new List<T> (IEnumerable<T> collection);
To create a set of parameters as a List
Eg:
string[] temArr = { "Ha", "Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
List<string> testList = new List<string>(temArr);
Adding elements:
1, List. Add (T item) adds an element
E.g.: mList.Add("John");
2, List. AddRange (IEnumerable <T> collection) adding a group of elements
Eg:
string[] temArr = { "Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", "Locu" };
mList.AddRange(temArr);
3, Insert (int index, T item); add an element at index
E.g.: mList.Insert(1, "Hei");
List traverse elements:
Type mList statement foreach (T element in mList) T when the same
{
Console.WriteLine(element);
}
Eg:
foreach (string s in mList)
{
Console.WriteLine(s);
}
Removing elements:
1, List. Remove (T item) to delete a value
E.g.: mList.Remove("Hunter");
. 2, List RemoveAt (int index); delete an element at index index
E.g.: mList.RemoveAt(0);
3、 List. RemoveRange(int index, int count);
Starting index index, delete count elements
E.g.: mList.RemoveRange(3, 2);
In determining whether an element in the List:
List. Contains (T item) returns true or false, it is practical
Eg:
if (mList.Contains("Hunter"))
{
Console.WriteLine("There is Hunter in the list");
}
else
{
mList.Add("Hunter");
Console.WriteLine("Add Hunter successfully.");
}
List to sort the elements inside:
List. Sort () is an element of a default letter ascending
E.g.: mList.Sort();
List to reverse the order of elements inside:
List. Reverse () can be List. Sort () used in conjunction to achieve the desired effect
E.g.: mList.Sort();
List empty:. List Clear ()
E.g.: mList.Clear();
List get the number of elements:
. List Count () returns an int value (by rotation Note: Found without "()")
Eg:
int count = mList.Count();
Console.WriteLine("The num of elements in the list: " +count);
(Turn by Note: Students learn basic stop here!)
2, List of advanced and powerful method:
List of example with:
string[] temArr = { Ha","Hunter", "Tom", "Lily", "Jay", "Jim", "Kuku", " "Locu" };
mList.AddRange(temArr);
List.Find Method: search conditions defined by the specified predicate match element, and returns the first matching element in the entire List.
public T Find(Predicate<T> match);
Predicate method is commissioned, if it is passed to the conditions defined in the delegate object match, the method returns true. List of current element is individually passed to the Predicate delegate, and move forward in the List, starting from the first element to the last element ends. When a match is found, processing stops.
Predicate can be delegated to a function or a lambda expression:
Entrusted to the lambda expression:
Eg:
string listFind = mList.Find (name => // name is variable, on behalf of mList
{// elements, set their own
if (name.Length > 3)
{
return true;
}
return false;
});
Console.WriteLine (listFind); // output is the Hunter
Delegate to a function:
Eg:
string listFind1 = mList.Find (ListFind); // function entrusted to ListFind
Console.WriteLine (listFind); // output is the Hunter
ListFind function:
public bool ListFind(string name)
{
if (name.Length > 3)
{
return true;
}
return false;
}
The results of these two methods is the same.
List.FindLast Method: search conditions defined by the specified predicate element matches, return the entire List and the last matching element.
public T FindLast(Predicate<T> match);
Usage and List.Find same.
List.TrueForAll Method: determining whether List each element conditions are defined by the specified predicate match.
public bool TrueForAll(Predicate<T> match);
Entrusted to the lambda expression:
Eg:
bool flag = mList.TrueForAll(name =>
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
}
);
Console.WriteLine("True for all: "+flag); //flag值为false
Delegate to a function, a function used here above ListFind:
Eg:
bool flag = mList.TrueForAll (ListFind); // function entrusted to ListFind
Console.WriteLine("True for all: "+flag); //flag值为false
The results of these two methods is the same.
List.FindAll Method: condition searching all elements defined by the specified predicate match.
public List<T> FindAll(Predicate<T> match);
Eg:
List <string> subList = mList.FindAll (ListFind); // function entrusted to ListFind
foreach (string s in subList)
{
Console.WriteLine("element in subList: "+s);
}
SubList storage time is greater than the length of all of the elements 3
List.Take (n): n rows returned value is obtained before IEnumetable <T>, T is the same type of List <T> type
Eg:
IEnumerable<string> takeList= mList.Take(5);
foreach (string s in takeList)
{
Console.WriteLine("element in takeList: " + s);
}
Then takeList element is stored in the first five mList
List.Where Method: condition searching all elements defined by the specified predicate match. List.FindAll with similar methods.
Eg:
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);
}
SubList storage time is greater than the length of all of the elements 3
List.RemoveAll Method: removal conditions with all elements defined by the specified predicate match.
public int RemoveAll(Predicate<T> match);
Eg:
mList.RemoveAll(name =>
{
if (name.Length > 3)
{
return true;
}
else
{
return false;
}
});
foreach (string s in mList)
{
Console.WriteLine("element in mList: " + s);
}
MList storage time is greater than the length of the element is removed after 3.
// the following important that those who turn
List is a generic list ... T represents a node element type
such as
List <int> intList; represents an element of the linked list int
intList.Add (34); // add
intList.Remove (34); // delete
intList.RemoveAt (0); // removes the element somewhere
intList.Count; // chain length
There Insert, Find, FindAll, Contains the like, there are indexing method intList [0] = 23;
1. Reduce the boxing and unboxing
2. facilitates compile-time type checking data
List equivalent System.Collections namespace inside the List
Killer quote from the blog, the original:
http://www.cnblogs.com/killers/archive/2011/08/28/2155920.html
Thank the author! Not view the copyright information, if infringement, please inform.