Definition, function, usage of C # generic list

Source: Internet
Author: User

Definition: The list class represents a strongly typed listing of objects 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:ilist,icollection,ienumerable,ilist,icollection,ienumerable
List is a generic equivalent class of the ArrayList class that implements the IList generic interface using an array of sizes that can be dynamically added on demand

(1) Declaration listmlist = new List ();
Eg:string[] Arr = {"A", "B", "C"};
List mlist = new list (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 students= new list ();
Students. ADD (Stu);
Students. ADD (New Student ("Candy"));
FindName myname = new FindName ("Arron");
foreach (student s in students. FINDALL (New predicate (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 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 match);

(5) List.take (n): Gets the first n rows the return value of Ienumetable,t is the same as the type of List

e.g.:

IEnumerable 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 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 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.

Definition, function, usage of C # generic list

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.